还有一天就过年了!今天还在为项目赶进度!
有一个模块需要根据城市经纬度定位出图标!问题来了,城市的数据哪里来?经纬度数据只要使用google或百度的API即可
在网上百般搜索也没有好的资源!后来想起之前某个网站项目借用TX的国际化文件数据,里面包含了世界主要城市名称!
完整中英文世界国家级联下拉列表插件【前端版】
http://my.oschina.net/Jacker/blog/339857
不过上次项目是转换成JSON使用!而且是按组划分!
这次我需要横向的数据队列,于是开始动手了!
<?php
set_time_limit(0);
echo '<pre>';
$path = dirname(__FILE__) . '/';
$file_cn = 'LocList_cn.xml';
$xml_cn = simplexml_load_file($path.$file_cn);
$file_en = 'LocList_en.xml';
$xml_en = simplexml_load_file($path.$file_en);
$file_continent = 'Location_continent.xml';
$xml_continent = simplexml_load_file($path.$file_continent);
/*大陆/洲*/
$ContinentResult = array();
$Continents = $xml_continent->xpath("//Continent");
foreach($Continents as $Continent) {
$_ContinentName = (String)$Continent->attributes();
$CountryRegions = $Continent->xpath("./CountryRegion");
foreach($CountryRegions as $CountryRegion) {
$_countryName = (String)$CountryRegion->attributes();
$ContinentResult[$_countryName][] = $_ContinentName;
}
}
//print_r($ContinentResult);
function xmlToArray($xml) {
$result = array();
$countryTime = 0;
$stateTime = 0;
$cityTime = 0;
$regionTime = 0;
foreach($xml->CountryRegion as $country) {
$countryName = (String)$country->attributes()['Name'];
$countryCode = (String)$country->attributes()['Code'];
$StateName = '';
$StateCode = '';
$cityName = '';
$cityCode = '';
$regionName = '';
$regionCode = '';
if (count($country) > 0) {
foreach($country as $state) {
$StateName = (String)$state->attributes()['Name'];
$StateCode = (String)$state->attributes()['Code'];
$cityName = '';
$cityCode = '';
if (count($state) > 0) {
foreach($state as $city) {
$cityName = (String)$city->attributes()['Name'];
$cityCode = (String)$city->attributes()['Code'];
$regionName = '';
$regionCode = '';
if (count($city) > 0) {
foreach($city as $region) {
$regionName = (String)$region->attributes()['Name'];
$regionCode = (String)$region->attributes()['Code'];
$result[$countryCode][] = array(
'country' => $countryName,
'country_code' => $countryCode,
'state' => $StateName,
'state_code' => $StateCode,
'city' => $cityName,
'city_code' => $cityCode,
'region' => $regionName,
'region_code' => $regionCode,
);
$regionTime += 1;
}
} else {
$result[$countryCode][] = array(
'country' => $countryName,
'country_code' => $countryCode,
'state' => $StateName,
'state_code' => $StateCode,
'city' => $cityName,
'city_code' => $cityCode,
'region' => $regionName,
'region_code' => $regionCode,
);
}
$cityTime += 1;
}
} else {
$result[$countryCode][] = array(
'country' => $countryName,
'country_code' => $countryCode,
'state' => $StateName,
'state_code' => $StateCode,
'city' => $cityName,
'city_code' => $cityCode,
'region' => $regionName,
'region_code' => $regionCode,
);
}
$stateTime += 1;
}
} else {
$result[$countryCode][] = array(
'country' => $countryName,
'country_code' => $countryCode,
'state' => $StateName,
'state_code' => $StateCode,
'city' => $cityName,
'city_code' => $cityCode,
'region' => $regionName,
'region_code' => $regionCode,
);
}
$countryTime += 1;
}
//echo $countryTime .'-'. $stateTime .'-'. $cityTime .'-'. $regionTime . '<br/>';
return $result;
}
$result_cn = xmlToArray($xml_cn);
/*调试*/
/*天津code21,中英数据不对称*/
//$country = $xml_en->xpath("//CountryRegion[@Code='1']");
//$state = $country[0]->xpath("./State[@Code='12']");
//$city = $state[0]->xpath("./City");
//print_r($city);exit;
/*Xpath查找英文对应数据*/
foreach($result_cn as $countryCode=>$rows) {
foreach($rows as $k=>$row) {
$country_en_name = '';
$state_en_name = '';
$city_en_name = '';
/*大洲*/
if (!empty($ContinentResult[$row['country']])) {
$result_cn[$countryCode][$k]['area_continent'] = $ContinentResult[$row['country']][0];
}
/*国家*/
if (!empty($row['country_code'])) {
$country = $xml_en->xpath("//CountryRegion[@Code='".$row['country_code']."']");
$country_en_name = (String)$country[0]->attributes()['Name'];
}
/*省级*/
if (!empty($country) && !empty($row['state_code'])) {
$state = $country[0]->xpath("./State[@Code='".$row['state_code']."']");
if (!empty($state)) {
$state_en_name = (String)$state[0]->attributes()['Name'];
}
}
/*市级*/
if (!empty($country) && !empty($row['city_code'])) {
$city = $state[0]->xpath("./City[@Code='".$row['city_code']."']");
if (!empty($city)) {
$city_en_name = (String)$city[0]->attributes()['Name'];
} else {
/*排除中国地区*/
if (!in_array($row['country_code'], array('1'))) {
$city = $country[0]->xpath("./State/City[@Code='".$row['city_code']."']");
if (!empty($city)) {
$city_en_name = (String)$city[0]->attributes()['Name'];
}
}
}
}
//echo $k.'-'.$country_en_name.'-'.$state_en_name.'-'.$city_en_name.'<br/>';
$result_cn[$countryCode][$k]['country_en'] = $country_en_name;
$result_cn[$countryCode][$k]['state_en'] = $state_en_name;
$result_cn[$countryCode][$k]['city_en'] = $city_en_name;
}
}
//print_r($result_cn);
//exit;
/*插入数据库*/
require_once 'sql_config.php';
$i = 0;
foreach($result_cn as $rows) {
foreach($rows as $row) {
/*排除字段*/
unset($row['country_code'],$row['state_code'],$row['city_code'],$row['region_code']);
/*内容过滤*/
$values = array_map(function ($var) {
return mysql_escape_string($var);
}, $row);
/*组装语句*/
$values = "'".implode("','",$values)."'";
$fields = "`".implode("`,`",array_keys($row))."`";
$sql = "INSERT INTO `trip_city_qq`({$fields})VALUES({$values})";
//mysql_query($sql);
$i += 1;
}
}
echo $i;
代码写得急,很臃肿!目标就是为了实现,有一个特别的地方就是英文版的数据中国地区只精确到市级!不像中文版能去到县级
国外基本都是国家然后就是城市!还有部分小国,只有国家名称!
以上省去解析了,主要用了 simplexml_load_file 和 xpath 组装和查找对应英文版的数据
然后再去用查找地理位置的API就搞定需求,这次我用了google! 国内使用时候需要翻墙,而且google限制了一个IP一天2500次的查询!
果阵我查到2400多条就停住了!换了IP就可以继续
echo '<pre>';
set_time_limit(0);
$this->load->model("cityModel");
$this->load->model("CityQQModel");
$this->load->model("countryModel");
$this->load->model("scenicModel");
/*更新地理信息(经纬度)*/
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=';
$citys = $this->CityQQModel->find();
foreach($citys as $city) {
if (empty($city['datetime'])) {
$query = $city['country']
.(!empty($city['state'])?'+'.$city['state']:'')
.(!empty($city['city'])?'+'.$city['city']:'')
.(!empty($city['region'])?'+'.$city['region']:'');
//echo $query;exit;
$result = file_get_contents($url.$query);
if ($result) {
$result = json_decode($result, true);
if ($result['status'] == 'OK') {
/*选取结果集最后一个*/
$location = $result['results'][count($result['results'])-1];
//print_r($location);exit;
$country_short = $location['address_components'][count($location['address_components'])-1]['short_name'];
$formatted_address = $location['formatted_address'];
$lat = $location['geometry']['location']['lat'];
$lng = $location['geometry']['location']['lng'];
$type = json_encode($location['types']);
if (!empty($country_short)) {
$where['country_short'] = $country_short;
}
if (!empty($formatted_address)) {
$where['formatted_address'] = $formatted_address;
}
if (!empty($lat) && !empty($lng)) {
$where['longitude'] = $lng;//经度
$where['latitude'] = $lat;//纬度
}
if (!empty($type)) {
$where['type'] = $type;
}
$where['datetime'] = date("Y-m-d H:i:s");
$this->CityQQModel->saveByID($city['cid'], $where);
sleep(1);
//exit;
}
}
}
}
上面的代码用了CI的框架
差点忘记告诉大家那个文件位置
C:\Program Files (x86)\Tencent\QQ\I18N\2052
具体语言包要看QQ,我是中英版本都装过一遍然后获取的!
另外还要一份是包含的按照“七大洲”划分的