使用[微信考勤百度地圖定位](http://blog.csdn.net/xuexiaodong009/article/details/48345445)中的方法定位,可以定位到一個具體的位置某省某市某區某路某號,總是讓人感覺顯示不是很友好,如果直接顯示,軟件園,科技園之類的是不是更好呢?于是查了一下百度地圖的相關文檔,其實也很簡單。百度有附近的功能,還有地址解析的功能,都可以實現。
例如我就是用了Geocoder服務,實現了需要的效果。
**核心代碼:**
~~~
var map = new BMap.Map("allmap");
var circle = new BMap.Geolocation();
circle.getCurrentPosition(locationResult); //
map.addOverlay(circle);
var tempGeocoder = new BMap.Geocoder();
function locationResult(geolocationResult) {
var Status = this.getStatus()
if (Status == 0)//檢索成功。對應數值“0”。
{
$("#lng").val(geolocationResult.point.lng);
$("#lat").val(geolocationResult.point.lat);
var address = geolocationResult.address;
$("#city").val(address.city);
$("#district").val(address.district);
$("#street").val(address.street);
var text = "";
if (address.province != address.city)
{
text += address.province;
}
text += address.city + address.district + address.street + address.street_number;
tempGeocoder.getLocation(geolocationResult.point, locationResultcallback, { poiRadius: 500, numPois: 5 }); //
$("#province").val(address.province);
$("#address").val(text);
}
else {
alert("定位失敗錯誤碼" + Status)
}
}
function locationResultcallback(GeocoderResult) {
var yyy = GeocoderResult.surroundingPois;
if (GeocoderResult.surroundingPois.length > 0) {
var address2 = GeocoderResult.surroundingPois[0].title;
if (address2) {
var text = "";
var province = $("#province").val();
var city = $("#city").val();
if (province != city) {
text += province;
}
text += city;
$("#address").val(text + address2);
}
}
}
~~~
實現效果:

**這樣總比顯示陜西省西安市雁塔區西三環好好的多吧。**