Adnan Hussein
Adnan Hussein

Reputation: 261

Can geocoder.geocode asynchronous calls be made synchronous? what change can be done in my code?

I have a function which adds marker on googlemap (v3) and returns an object with result related where it added the marker. Based on the success value returned from this function i need to write some logic. But since geocoder.geocode executes asynchronously i have the result returned as false which fails my logic. Is there a way i can do it synchronously? Here is my code:

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

Upvotes: 1

Views: 2159

Answers (2)

Adnan Hussein
Adnan Hussein

Reputation: 261

Here is what i did to resolve, changing the callback routine for my logic:

$("#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;

});

Upvotes: 0

geocodezip
geocodezip

Reputation: 161334

The geocoder is asynchronous and provides a callback for a reason (to free up the browser to do other things while waiting for a response from the server). Structure your code to use the data in the call back routine (when it is available).

Upvotes: 1

Related Questions