公告
淡泊明志,宁静致远
网站资讯
本站文章字数合计
267.4k
本站Hexo版本
6.1.0
本站Node版本
20.19.6
本站已运行时间
最后更新时间
本文目录
已阅读:%

分类: web前端 | 标签: 定位 地图

百度地图获取定位

发表于: 2024-06-26 16:48:28 | 字数统计: 310 | 阅读时长预计: 1分钟

实现代码

第一步:获取经纬度

第二步:根据经纬度调用百度获取地址

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        html {
            height: 100%
        }
        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
        #container {
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="container"></div>
    <script src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=你的百度ak"></script>
    <script>
        var map = new BMapGL.Map("container");
        var point = new BMapGL.Point(116.404, 39.915);
        map.centerAndZoom(point, 15);
        var geolocation = new BMapGL.Geolocation();
        geolocation.enableSDKLocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var mk = new BMapGL.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                alert('您的位置:' + r.point.lng + ',' + r.point.lat);
                // 创建地理编码实例      
                var myGeo = new BMapGL.Geocoder();
                // 根据坐标得到地址描述    
                myGeo.getLocation(new BMapGL.Point(r.point.lng, r.point.lat), function (result) {
                    if (result) {
                        alert(result.address);
                    }
                });
            }
            else {
                alert('failed' + this.getStatus());
            }
        });
    </script>
</body>
</html>

参考

正逆地址解析:https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/geocoding
定位:https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/geoloaction

------ 本文结束,感谢您的阅读 ------
本文作者: 程序员青阳
版权声明: 本文采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。