想将数据表中新加字段经纬度,对应城市名称,选用腾讯位置api(http://lbs.qq.com/webservice_... 遍历城市获取经纬度值,条用过程中发现好多城市不返回经纬度值(实际上有值(url测试过)),,返回的值4个一连续,然后就断了,不知道是不是因为,并发限制:5次/秒/接口/Key的原因。
function api2($city){
$url = sprintf('http://apis.map.qq.com/ws/geocoder/v1/?'.'address=%s&key=DBUBZ-VPLKI-RESGX-5RSER-VCWSK-V2BHK', $city);
$json = http2($url);
$data = json_decode($json, JSON_UNESCAPED_UNICODE);
if ($data === FALSE) {
return FALSE;
}
if (isset($data['status']) && $data['status'] == 0 && isset($data['result']) && !empty($data['result'])) {
return $data['result']['location'];
}
}
$temp = array();
foreach($citys2 as $k=>$v){
// echo gettype($v['address']);
$a = api2($v['address']);
if(isset($a)){
$location = $a['lng'] .','.$a['lat'];
}
else{
$location = '';
}
$temp[$v['id']] = $location;
}
foreach($temp as $k=>$v){
update_map($k,$v);
}
function update_map($address, $id){
$conn=@mysql_connect("localhost:3306","root","123456")or die ("mysql链接失败");
//第二步: 选择指定的数据库,设置字符集
@mysql_select_db("idollar",$conn) or die ("db链接失败".mysql_error());
mysql_query('SET NAMES UTF8')or die ("字符集设置错误");
$sql = "update map set point = '".$point."' where id = '".$id."'";
$result = mysql_query($sql);
if (!$result) {
throw new Exception('Could not execute query');
}
else{
return true;
}
}