发布于 5年前

js 利用百度地图接口在浏览器中获取经纬度

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6yAoynmTPNlTBa8z1X4LfwGE"></script>
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>

    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            currentLat = r.point.lat;
            currentLon = r.point.lng;
            console.log(currentLat);
            console.log(currentLon);

            var pt = new BMap.Point(currentLon, currentLat);
            var geoc = new BMap.Geocoder();

            geoc.getLocation(pt, function (rs) {
                var addComp = rs.addressComponents;
                console.log(addComp);

                var city = addComp.city;
                var addComp = rs.addressComponents;
                var texts = addComp.district + " " + addComp.street + " " + addComp.streetNumber;
                //获取地理位置成功,跳转
                console.log(texts);
            });
        }
    });
©2020 edoou.com   京ICP备16001874号-3