问题描述
- 百度地图为什逆解析地址没反应
-
var MapLng; var MapLat; map.removeOverlay(marker); geo.getPoint(obj.value, function (point) { if (point) { map.centerAndZoom(point, 15); marker = new BMap.Marker(point); map.addOverlay(marker); MapLng = point.lng; MapLat = point.lat; } }, '上海'); var ply = new BMap.Polygon(pts); var pt = new BMap.Point(MapLng, MapLat); geo.getLocation(pt, function (rs) { var addComp = rs.addressComponents; alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); });
没有反应,各位求帮忙
解决方案
解码是异步的, geo.getPoint(obj.value, function (point) {这个回调还没执行就已经执行 geo.getLocation(pt, function (rs) {这句了,导致pt变量没有值。解码要放到geo.getPoint(obj.value, function (point) {回调里面
var MapLng;
var MapLat;
map.removeOverlay(marker);
geo.getPoint(obj.value, function (point) {
if (point) {
map.centerAndZoom(point, 15);
marker = new BMap.Marker(point);
map.addOverlay(marker);
MapLng = point.lng;
MapLat = point.lat;
pointToAddress(); ////////
}
}, '上海');
var ply = new BMap.Polygon(pts);
function pointToAddress() {
var pt = new BMap.Point(MapLng, MapLat);
geo.getLocation(pt, function (rs) {
var addComp = rs.addressComponents;
alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});
}
时间: 2024-11-02 12:27:27