高德地图逆地址编码

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 @   孤陌  阅读(610)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示