mirror of https://github.com/dnomd343/echoIP
Dnomd343
4 years ago
3 changed files with 61 additions and 9 deletions
Binary file not shown.
@ -0,0 +1,44 @@ |
|||
<?php |
|||
|
|||
// 数据来源:ChinaCity.db |
|||
// 请求方式:getLoc($region, $city) |
|||
// 返回格式: |
|||
// { |
|||
// "region": 省/自治区/直辖市名称 |
|||
// "city": 市/自治州名称 |
|||
// "lat": 地区纬度 |
|||
// "lon": 地区经度 |
|||
// } |
|||
|
|||
class cityDB extends SQLite3 { |
|||
function __construct() { |
|||
$this->open('ChinaCity.db'); // 中国省市经纬度数据库 |
|||
} |
|||
} |
|||
|
|||
function getLoc($region, $city) { // 根据省份/城市信息查询经纬度 |
|||
$db = new cityDB; |
|||
$data['region'] = $region; |
|||
$data['city'] = $city; |
|||
$query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="'.$city.'";'; |
|||
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); |
|||
if (!$raw) { // 查无数据 |
|||
$query_str='SELECT * FROM main WHERE level1="'.$region.'" AND level2="-";'; // 尝试仅查询省份数据 |
|||
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); |
|||
if (!$raw) { // 省份错误,返回北京经纬度 |
|||
$data['lat'] = '39.91'; |
|||
$data['lon'] = '116.73'; |
|||
return $data; |
|||
} |
|||
if ($city == '') { |
|||
$query_str='SELECT * FROM main WHERE level1="'.$region.'" LIMIT 1,1;'; // 获取省会记录 |
|||
$raw = $db->query($query_str)->fetchArray(SQLITE3_ASSOC); |
|||
$data['city'] = $raw['level2']; |
|||
} |
|||
} |
|||
$data['lat'] = $raw['lat']; |
|||
$data['lon'] = $raw['lon']; |
|||
return $data; |
|||
} |
|||
|
|||
?> |
Loading…
Reference in new issue