Skip to Content

issue in geocoder.geocode() to get a latitude and longitude.

Hello Experts,

I have a requirement to display location as a marker on the google map and the address are coming from the backed.

Now i have used for loop to take address one by one and pass to the method geocoder.geocode().

Now i can see that when the control come to this method, instead of going to the inner side of method it is going back to loop.

following is the code:--

for (var i = 0; i <= oData.results.length; i++)

{ var address = oData.results[i].ZADDRESS;

that.geocoder = new google.maps.Geocoder();

that.geocoder.geocode({ 'address': address },

function(results, status)

{ if (status == google.maps.GeocoderStatus.OK)

{ results[0].geometry.location.lat();

results[0].geometry.location.lng();

}});


Can you please tell me why control is not going into geocoder.geocode() ?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    May 16 at 09:52 AM

    Hi Deborshi De Sarkar,

    If the data comes from back-end is oData, then you can simply use data binding. For example see GeoMap with Spots, and forget the loop.

    Else please provide a GIT repository or attach a .zip file to see what's going on in the code.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Miklos Sebestyen ,

      Thanks for this information.But i need to show direction from source to destination and data which is coming from the back end as a address like "Eiffel Tower, 5 Avenue Anatole France". So i think i have to convert address to latitude and longitude.After getting this i can display marker in google map.Please correct me if i am wrong.
      ThanksDeborshi
  • avatar image
    Former Member
    May 17 at 08:51 AM

    Ok, I see now, so you only have the address of a location and using Google API to retrieve the geocode.

    The possible reason for not seeing the control going into "geocode()" callback function is the asynchronous behaviour of the calls. If the call never runs into the callback function then check the console log of the browser, that might be some network error.

    You should also output if the status is not OK. as the Google API documentation states, that might include some useful information.

    function codeAddress() {
      var address = document.getElementById("address").value;
      geocoder.geocode({ address: address }, function(results, status) {
        if (status == "OK") {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
    Add comment
    10|10000 characters needed characters exceeded