天天看點

簡單使用高德地圖開放平台API

需求說明

輸入經緯度,得到城市名

挑選API

使用高德逆地理編碼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;
        }
    }