// 根据经纬度获取城市
public function getCityByLongLat() {
$data = file_get_contents('php://input'); // 接收过来的json数据
$input = json_decode($data, true);// 转化为数组
if (empty($input['lon']) || !isset($input['lon']) || empty($input['lat']) || !isset($input['lat'])) {
return $this->json_data(array('st' => 0, 'msg' => '缺少必要参数'));
}
$lon = $input['lon'];
$lat = $input['lat'];
// lon 经度 lat 纬度
if ($lon == '' || $lat == '') return '';
$url = "http://api.map.baidu.com/geocoder?location={$lon},{$lat}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
$result = $this->XmlToArr($output);
return $this->json_data(array('st' => 1, 'msg' =>'获取成功','city'=>$result['result']['addressComponent']['city']));
}
网友评论