How do you return a latitude and longitude array using the Google Javascript v3 Geocoder? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-25 02:57:31

问题


I'm trying to create a function that utilizes Google Javascript V3's geocoding capabilities and returns an array with the longitude and latitude. For some reason the return array is not being populated using the function. Thanks for your help!

Code:

  function getCoords(address) {
    var latLng = [];
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        latLng.push(results[0].geometry.location.lat());
        latLng.push(results[0].geometry.location.lng());
        return latLng;
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }    
    });
  }

  var test_arr;    
  test_arr = getLatLng('New York');
  alert(test_arr[0] + ',' + test_arr[1]) // I'm getting a test_arr is undefined here.

回答1:


Read up on using callback functions in Javascript. This article might be helpful.

As Jon pointed out, you can solve this by passing a callback function into your getCoords method. It's a way of waiting for the response to come back from Google. You define a function that will be called when the geocoding is done. Instead of returning the data, you'll call the provided function with the data as an argument.

Something like this:

function getCoords(address, callback) {
  var latLng = [];
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      latLng.push(results[0].geometry.location.lat());
      latLng.push(results[0].geometry.location.lng());
      callback(latLng);
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }    
  });
}

getCoords('New York', function(latLng) {
  var test_arr;
  test_arr = latLng;
  alert(test_arr[0] + ',' + test_arr[1])
  // Continue the rest of your program's execution in here
});



回答2:


@Matt Ball should have posted the answer. :) The reason test_arr is undefined is because you're evaluating it immediately before the results come back.

If you did a setTimeout (which you shouldn't do), you would notice eventually the array will have something in it.

setTimeout(function(){
   alert(test_arr) // has stuff...
}, 5000);

Instead, you can pass an anonymous function to getCoords as a callback. This function gets executed once the coordinates are available.

function getCoords(address, callback) {
    ...
    var lng = results[0].geometry.location.lng();
    var lat = results[0].geometry.location.lat();
    callback([lat, lng]);
    ...
}

getCoords("New York", function(coords) {
  alert(coords); // works
});


来源:https://stackoverflow.com/questions/14794428/how-do-you-return-a-latitude-and-longitude-array-using-the-google-javascript-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!