简体   繁体   中英

Return Latitude and Longitude from Google API and pass to Array

I simply want to return longitude and latitude and push them to empty latitude and longitude arrays. My problem is that when I alert(lat[0]), it comes up undefined and I want to be able to access these values instead of just using an alert in a callback function. Is there any way around this. Any help is greatly appreciated!

var lat=[];
var lon=[];
var geocoder =  new google.maps.Geocoder();
geocoder.geocode( { 'address': 'miami, us'}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        lat.push(results[0].geometry.location.lat()); 
        lon.push(results[0].geometry.location.lng()); 
      } else {
        alert("Something got wrong " + status);
      }
    });
alert(lat[0]);
alert(lon[0]);

@AndrewEvt is right. Just add this before the last two lines of the code you posted: ... geocoder(); //This calls your geocoder function
alert(lat[0]);
alert(long[0]);

I ended up solving the issue myself. My solution takes in a list of cities and geocodes them through the API calls. When the last call is brought back, a callback function called Main runs. The function Main is where you can access the populated array of lats and lons.

//Here is an array of locations to be geocoded
var locations1 = [];
locations1.push("Horsham, PA");
locations1.push("Dallas, TX");
locations1.push("Chicago, IL");
locations1.push("Denver, CO");
locations1.push("San Diego, CA");

// Create an array to store the latitudes and longitudes
var lat1=[];
var lon1=[];

//create time parameter to delay each call, limited number of calls per 
//second
var time=0;

//loop through each location and call the geo function 
for (var i = 0; i < locations1.length; i++) {
  geo(locations1[i],time);
  time=time+1;
}

// inputs a location and time parameter then adds the lat/lon to array
function geo(loc,t){

  var geocoder = new google.maps.Geocoder();
  setTimeout(function(){
    geocoder.geocode({
      'address': loc
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        lat1.push(results[0].geometry.location.lat());
                    lon1.push(results[0].geometry.location.lng());

      } else {
        alert("Something got wrong " + status+ " problem with: "+ loc);
      }
      // When your array is full run main, this is where you can do 
//things with the array
      if(lat1.length==locations1.length){
        main();

      }

    });
  },t*1000);
}

function main(){
  for(var j=0;j<lat1.length;j++){
     alert(locations1[j]+" has latitude of "+lat1[j]+", and longitude of 
     "+lon1[j]);
     // do what ever else you want to do with the populated array
  }
}

Here is a fiddle that has all the code:

http://jsfiddle.net/sharder14/afkzv2fx/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM