高德地图逆地址编码

JavaScript

<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=高德地图key&plugin=AMap.Geocoder"></script>
<script>
  $("#xi-address").on('click',function(){
    var address = $("#address").val();
    layer.msg('地址解析中...');
      AMap.plugin('AMap.Geocoder', function() {
        var geocoder = new AMap.Geocoder({ })
        geocoder.getLocation( address, function(status, result) {
          if (status === 'complete' && result.info === 'OK') {
            layer.msg('地址解析成功!');
            var x = result.geocodes['0'].location.lng;
            var y = result.geocodes['0'].location.lat;
            $("#lng").val(x+','+y);
          } else {
            layer.msg('您输入的地址没有解析到结果!');
          }
        })
      })
  });
    
</script>

PHP 

/**
 * curl请求指定url (get)
 * @param $url
 * @param array $data
 * @return mixed
 */
function curl($url, $data = [])
{
    // 处理get数据
    if (!empty($data)) {
        $url = $url . '?' . http_build_query($data);
    }
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

/**
 * 逆地理编码
 * @param string 地址信息
 * @return mixed
 */
function enverseGeocoding($msg = '')
{
    $url = 'https://restapi.amap.com/v3/geocode/geo';

    $result = curl($url, ['key' => config('amap_key'), 'address' => $msg]);
    $json_ar = json_decode($result, true);
    return isset($json_ar['geocodes'][0]['location']) && !empty($json_ar['geocodes'][0]['location']) ? $json_ar['geocodes'][0]['location'] : '';
}

 

posted @ 2020-12-09 22:32  孤陌  阅读(579)  评论(0编辑  收藏  举报