1

我有一个函数可以在 googlemap (v3) 上添加标记,并返回一个与添加标记的结果相关的对象。根据此函数返回的成功值,我需要编写一些逻辑。但是由于 geocoder.geocode 异步执行,我将结果返回为 false,这使我的逻辑失败。有没有办法我可以同步做到这一点?这是我的代码:

 addMarkerAtGMap = function (positionParam, gMapObject, colorMarkerObj) {
       if (infoGMapMarkerWin !== undefined && infoGMapMarkerWin.open !== undefined) {
           infoGMapMarkerWin.close();
       }


// Set the success as false by default..here due to async call the success value is incorrectly returned as false 

 returnGMapObject = { success: false, latitude: "0", longitude: "0", gMapObject: gMapObject };     

       geocoder.geocode({ 'address': positionParam }, function (results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
               map.setCenter(results[0].geometry.location);
               gMapObject = new google.maps.Marker({
                   icon: colorMarkerObj.markerImage,
                   shadow: colorMarkerObj.markerShadow,
                   position: results[0].geometry.location
               });
               gMapObject.setMap(map);
               google.maps.event.addListener(gMapObject, "click", function (event) {

                   if (infoGMapMarkerWin !== undefined && infoGMapMarkerWin.open !== undefined) {
                       infoGMapMarkerWin.close();
                   }
                   infoGMapMarkerWin = new google.maps.InfoWindow(
                        { content: positionParam,
                            size: new google.maps.Size(100, 100),
                            position: gMapObject.position
                        });
                   infoGMapMarkerWin.open(map);
               });
               returnGMapObject = { success: true, latitude: results[0].geometry.location.Xa, longitude: results[0].geometry.location.Ya, gMapObject: gMapObject };
           }
       });
       return returnGMapObject;
   }

$("#btnAddLocation").click(function () {
if (!PLVM.isValid()) {
    showMessage(PLVM.errors());
}
else {
    var address = $("#Addressline1").val();
    var colorMarkerObj = $Utils.colorMapMarker("FF8277");       // Add color to marker
    var gMapObject = $Utils.addMarkerAtGMap(address, locMarkerObj, colorMarkerObj);
    if (gMapObject.success == true) { 
// gMapObject.success returned as false due to asynchronous call which fails my logic 
        if (PLVM.Latitude() == gMapObject.latitude && PLVM.Longitude() == gMapObject.longitude) {
            locMarkerObj.setMap(null);
        }
        else {
            PLVM.Latitude(gMapObject.latitude);
            PLVM.Longitude(gMapObject.longitude);
            PartnerDetailsVM.addPL(PLVM);
        }
    }
}
return false;});
4

2 回答 2

1

地理编码器是异步的,并出于某种原因提供回调(以在等待服务器响应时释放浏览器以执行其他操作)。构建代码以使用回调例程中的数据(如果可用)。

于 2012-10-04T19:02:20.557 回答
0

这是我为解决我的逻辑更改回调例程所做的工作:

$("#btnAddLocation").click(function () {

if (!PLVM.isValid()) {
    showMessage(PLVM.errors());
}
else {
    var address = $("#Addressline1").val();

    var colorMarkerObj = $Utils.colorMapMarker("FF8277");       // Add color to marker

    $Utils.addMarkerAtGMap(address, locMarkerObj, colorMarkerObj, function (gMapObject) {

        if (gMapObject !== undefined && gMapObject.gMapObject !== undefined) {
            locMarkerObj = gMapObject.gMapObject;
        }
        if (gMapObject !== undefined && gMapObject.success) {
            markArr.push(locMarkerObj);
            PLVM.Latitude(gMapObject.latitude);
            PLVM.Longitude(gMapObject.longitude);
            PartnerDetailsVM.addPL(PLVM);
        }

    });
}
return false;

});

于 2012-10-05T07:16:28.780 回答