Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle maps geocode API V3 not returning result in javascript function
    primarykey
    data
    text
    <p>I'm trying to use the Google geocoder API V3 to plot a location on a map based on an address specified by the user, code is below. </p> <p>When I make a request directly (e.g. to <a href="http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&amp;sensor=false" rel="nofollow noreferrer">http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&amp;sensor=false</a>) I get the expected response. However, when I make the same request using the code below, the <em>midpoint</em> variable is always undefined after the <em>getLatLong</em> function has exited.</p> <p>What am I doing incorrectly?</p> <pre><code>function loadFromSearch(address) { midpoint = getLatLong(address); mapCentre = midpoint; map.setMapTypeId(google.maps.MapTypeId.ROADMAP); ... } function getLatLong(address) { var result; var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&amp;sensor=false' $.getJSON(url, function (data){ if (data.status == "OK") { result = data.results[0].geometry.location; } }); return result; } </code></pre> <p>==================================================================================</p> <p>In light of responses, I have now updated the code to the following. I'm still not getting any result though, with breakpoints set in Firebug the <em>result = data.results[0].geometry.location;</em> never gets hit.</p> <pre><code>function loadFromSearch(address) { midpoint = getLatLong(address, loadWithMidpoint); } function getLatLong(address, callback) { var result; var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&amp;sensor=false' $.getJSON(url,{}, function (data) { if (data.status == "OK") { result = data.results[0].geometry.location; callback(result); } }); } function loadWithMidpoint(centre) { mapCentre = centre; map.setMapTypeId(google.maps.MapTypeId.ROADMAP); ... } </code></pre> <p>============================================================================= </p> <p>I have it! The final code, which works, looks like this:</p> <pre><code>function loadFromSearch(coordinates, address) { midpoint = getLatLong(address, latLongCallback); } function getLatLong(address, callback) { var geocoder = new google.maps.Geocoder(); var result = ""; geocoder.geocode({ 'address': address, 'region': 'uk' }, function (results, status) { if (status == google.maps.GeocoderStatus.OK) { result = results[0].geometry.location; latLongCallback(result); } else { result = "Unable to find address: " + status; } }); return result; } function latLongCallback(result) { mapCentre = result; map.setMapTypeId(google.maps.MapTypeId.ROADMAP); ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload