2012-10-04 55 views
1

我有一個函數在googlemap(v3)上添加標記,並返回一個對象,其結果與添加標記的位置相關。基於從這個函數返回的成功值,我需要編寫一些邏輯。但由於geocoder.geocode異步執行,我將結果返回爲false,這導致我的邏輯失敗。有沒有辦法我可以同步做到這一點?這裏是我的代碼:geocoder.geocode異步調用可以同步嗎?我的代碼中可以做些什麼改變?

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;}); 

回答

1

地址解析器是異步的,並提供了一個理由回調(以釋放瀏覽器做其他的事情在等待服務器的響應)。構建您的代碼以使用回調例程中的數據(當它可用時)。

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; 

});

相關問題