解码是异步的, 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);
});
}