Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从GoogleAPI返回纬度和经度并传递到数组_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 从GoogleAPI返回纬度和经度并传递到数组

Javascript 从GoogleAPI返回纬度和经度并传递到数组,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我只想返回经度和纬度,并将它们推送到空的纬度和经度数组中。我的问题是,当我发出警报(lat[0])时,它没有定义,我希望能够访问这些值,而不仅仅是在回调函数中使用警报。这有什么办法吗。非常感谢您的帮助 var lat=[]; var lon=[]; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': 'miami, us'}, function(results, status) { if

我只想返回经度和纬度,并将它们推送到空的纬度和经度数组中。我的问题是,当我发出警报(lat[0])时,它没有定义,我希望能够访问这些值,而不仅仅是在回调函数中使用警报。这有什么办法吗。非常感谢您的帮助

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]);

@安德烈是对的。只需在您发布的最后两行代码之前添加以下内容: ... 地理编码器()//这将调用您的地理编码器功能
警报(纬度[0])

警报(长[0])

最后我自己解决了这个问题。我的解决方案接收城市列表,并通过API调用对它们进行地理编码。当最后一个调用返回时,将运行一个名为Main的回调函数。在Main函数中,您可以访问已填充的LAT和LON数组

//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
  }
}
//这里是要进行地理编码的位置数组
var位置1=[];
位置1.推送(“宾夕法尼亚州霍舍姆”);
地点1.push(“德克萨斯州达拉斯”);
地点1.push(“伊利诺伊州芝加哥”);
地点1.普什(“科罗拉多州丹佛市”);
地点1.push(“加利福尼亚州圣地亚哥”);
//创建一个数组来存储纬度和经度
var lat1=[];
var lon1=[];
//创建时间参数以延迟每次呼叫,限制每次呼叫的次数
//第二
var时间=0;
//循环遍历每个位置并调用geo函数
对于(变量i=0;i对于(var j=0;jis
geocode
method sync?还是异步?您正在尝试访问尚未定义的变量。这是因为
geocoder中的
callback
函数。geocode
尚未调用。因此您会得到
未定义的
。请尝试在
else
语句的右括号后立即替换您的警报-然后您就可以了将查看您的CoordsHanks以获得快速响应!但它仍然不起作用。显然它是异步的。此外,如果我将警报放在else语句之后,警报会起作用,但它们不会超出地理代码块(我需要访问它们的位置)感谢您的快速响应!它仍然不起作用。显然它是异步的。此外,如果我将警报放在else语句之后,警报会起作用,但它们不会超出地理代码块(我需要在该块中访问它们)。