需求说明
输入经纬度,得到城市名
挑选API
demo
<?php
/**
* 根据输入的经纬度返回城市名称
* @param $longitude 终点坐标(经度)
* @param $dimension 终点坐标(纬度)
* @return string
*/
public function getCity($longitude,$dimension)
{
$location = $longitude . ',' . $dimension;//坐标
$key = '6b3*************************6995';//高德地图key-web
$curl = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=' . $location . '&key=' . $key . '&radius=1000&extensions=base';
$content = file_get_contents($curl);
$result = json_decode($content, true);
if ($result['status'] == 0) {
return '接口请求错误,请检查参数是否正确';
} else {
$city = $result['regeocode']['addressComponent']['city'];
return $city;
}
}