百度地图JS获取当前所在城市信息

function getCity() {
    var latlon = null;
    //ajax获取用户所在经纬度
    $.ajax({
        url: "http://api.map.baidu.com/location/ip?ak=YourAK&coor=bd09ll",
        type: "POST",
        dataType: "jsonp",
        success: function (data) {
            latlon = data.content.point.y + "," + data.content.point.x;
            //ajax根据经纬度获取省市区
            $.ajax({
                type: "POST",
                dataType: "jsonp",
                url: 'http://api.map.baidu.com/geocoder/v2/?ak=YourAK&callback=renderReverse&location=' + latlon + '&output=json&pois=0',
                success: function (response) {
                    if (response.status == 0) {
                        console.log(response.result.addressComponent.province + "-" + response.result.addressComponent.city + "-" + response.result.addressComponent.district);
                        city = response.result.addressComponent.city;
                    }
                }
            });
        }
    });
}

你可能感兴趣的:(前端)