高德地图获取当前地理详细位置信息

 
更多

调用高德地图api获取当前地理位置的详细信息。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>浏览器精确定位</title>
<body>
<p id="result1"></p>
<script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=1d94daed62ea1f37371f44bc151224b2&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
    AMap.plugin('AMap.Geolocation', function () {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位,默认:true
            timeout: 10000,          //超过10秒后停止定位,默认:5s
            useNative: true
        });
        geolocation.getCurrentPosition(function (status, result1) {
            if (status == 'complete') {alert(JSON.stringify(result1));
                var geocoder = new AMap.Geocoder({radius: 500}),
                    value = result1.position.lng + ',' + result1.position.lat;
                geocoder.getAddress(value.split(','), function (status, result) {
                    if (status === 'complete' && result.regeocode) {
                        document.getElementById('result1').innerHTML = result.regeocode.formattedAddress;
                        //加载天气查询插件
                        AMap.plugin('AMap.Weather', function() {
                            //创建天气查询实例
                            var weather = new AMap.Weather();alert(result1.addressComponent.adcode);
                            //执行实时天气信息查询
                            weather.getLive(result1.addressComponent.adcode, function(err, data) {
                                console.log(err, data);
                                alert(data.weather);
                            });
                        });
                    } else {
                        alert('根据经纬度查询地址失败')
                    }
                });
            } else {
                alert('定位失败,原因:' + result.message);
            }
        });
    });
</script>
</body>
</html>
打赏

本文固定链接: https://www.cxy163.net/archives/3159 | 绝缘体

该日志由 绝缘体.. 于 2021年12月03日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 高德地图获取当前地理详细位置信息 | 绝缘体
关键字: , , , ,

高德地图获取当前地理详细位置信息:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter