需求說明
輸入經緯度,得到城市名
挑選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;
}
}