简单使用高德地图开放平台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;
        }
    }
posted @ 2020-06-23 16:00  为牧  阅读(2927)  评论(0编辑  收藏  举报