//这里为调用的方法
function GetAddress() {
GetCurrentAddress(function(result) {
$("#sy_address").val(result.address);
});
}
var CurrentAddressCallBack = null;
var CurrentAddressMap = null;
function GetCurrentAddressInit() {
CurrentAddressMap = true; // 创建Map实例
GetCurrentAddressLocation(CurrentAddressCallBack);
}
function GetCurrentAddress(callBack) {
if (CurrentAddressMap == null) {
CurrentAddressCallBack = callBack;
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://api.map.baidu.com/api?v=2.0&ak=TG3Bk7cGh0Tp2te0uUVG2gdy0m6N7et8&callback=GetCurrentAddressInit";
document.body.appendChild(script);
} else {
GetCurrentAddressLocation(callBack);
}
}
function GetCurrentAddressLocation(callBack) {
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
// 创建地理编码实例
var myGeo = new BMap.Geocoder();
// 根据坐标得到地址描述
myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), function (result) {
if (result) {
callBack(result);
} else {
alert("位置获取失败!请重试");
}
});
} else {
alert('failed' + this.getStatus());
}
}, { enableHighAccuracy: true });
}