Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

jquery - Google maps geocode API V3 not returning result in javascript function

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.

When I make a request directly (e.g. to http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false) I get the expected response. However, when I make the same request using the code below, the midpoint variable is always undefined after the getLatLong function has exited.

What am I doing incorrectly?

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) + '&sensor=false'
  $.getJSON(url,
  function (data){
     if (data.status == "OK")
     {
        result = data.results[0].geometry.location;
     }
  });
  return result;
}

==================================================================================

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 result = data.results[0].geometry.location; never gets hit.

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) + '&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);
  ...
}

=============================================================================

I have it! The final code, which works, looks like this:

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);
  ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are using V3 of the API cannot you use the this?

function findAddressViaGoogle() {
    var address = $("input[name='property_address']").val();
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            newPointClicked(results[0].geometry.location)
        } else {
            alert("Unable to find address: " + status);
        }
    });
}

The above is what I use to find some lat long cordinates of an inputted address, May work better?

EDIT:

function loadFromSearch(address) 
{
midpoint = getLatLong(address);
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}


function getLatLong(address) 
{
    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;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    return result;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...