百度地圖定位,獲取省市區

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.min.js"></script>
    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=v"></script>
</head>
<body>
    <div id="allmap"></div>
</body>
<script type="text/javascript">
    $(function () {
        var geolocation = new BMap.Geolocation();
        var gc = new BMap.Geocoder();
        geolocation.getCurrentPosition(function (r) {
            console.log(r);
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var pt = r.point;
                gc.getLocation(pt, function (rs) {
                    var addComp = rs.addressComponents;
                    var province = addComp.province;
                    var city = addComp.city;
                    var area = addComp.district;
                    alert(province+":"+city+":"+area);

                });
            }
            else {
                alert("定位失敗");
            }
        }, { enableHighAccuracy: true });
    });
    //獲取地裏位置 End
</script>
</html>
相關文章
相關標籤/搜索