将地理编码器结果保存到数组 - 关闭问题 [英] Saving geocoder results to an array - Closure Trouble

查看:14
本文介绍了将地理编码器结果保存到数组 - 关闭问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经搜索了一段时间来寻找解决此问题的方法,但我没有找到任何具体的解决方案.在您向我指出 Google 的服务条款之前,请阅读问题的结尾!

Okay, so I have searched a while for a solution to this problem, but I have found nothing specifically on this. And before you point me to Google's Terms of Service, please read to the ende of the question!

所以这里的想法:我想使用 Google 的地理编码器将地址的纬度和经度保存到数组中.我设法正确计算了所有值,但似乎无法将其保存到数组中.我已经使用匿名函数将地址传递给函数,但是保存仍然不起作用.请帮忙!

So here's the idea: I want to save the Latitude and Longitude of an address into an array using Google's Geocoder. I managed to calculate all the values correctly, but I just can't seem to save it to the array. I have already used an anonymous function to pass the address to the function, but the saving is still not working. Please help!

关于 Google 的服务条款:我知道我可能不会将此代码保存在任何地方,也不会显示在 Google 地图中.但我需要将其保存为 kml 文件,以便稍后输入 Google 地图.我知道,只创建 Map 会方便得多,但由于其他原因,这是不可能的.

Regarding Google's Terms of Service: I know that I may not save this code anywhere and not display it in a Google Map. But I need to save it as a kml-file to feed into a Google Map later. I know, it would be much more convenient to just create the Map, but for other reasons that's not possible.

adressdaten[] 是一个二维数组,其中包含地址数据这是代码:

adressdaten[] is a two-dimensional array with the data of the addresses This is the code:

for (i=1; i<adressdaten.length-1; i++)  {
//Save array-data in String to pass to the Geocoder
var adresse = adressdaten[i][3] + " " + adressdaten[i][4];
var coordinates;
var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': adresse}, (function (coordinates, adresse) {
        return function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
               var latLong = results[0].geometry.location;
               coordinates = latLong.lat() + "," + latLong.lng();


        } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        }
    })(coordinates, adresse));
    adressdaten[i][6] = coordinates;
}

推荐答案

这是一个常见问题.地理编码是异步的.您需要将结果保存在从服务器返回时运行的回调函数中.

This is a FAQ. Geocoding is asynchronous. You need to save the results in the callback function which runs when they are returned from the server.

类似(未测试)

更新为使用函数闭包

function geocodeAddress(address, i) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       var latLong = results[0].geometry.location;
       coordinates = latLong.lat() + "," + latLong.lng();
       adressdaten[i][6] = coordinates;
    } else {
       alert('Geocode of '+address+' was not successful for the following reason: ' + status);
    }
  });
}

var geocoder = new google.maps.Geocoder();
for (i=1; i<adressdaten.length-1; i++)  {
  //Save array-data in String to pass to the Geocoder
  var adresse = adressdaten[i][3] + " " + adressdaten[i][4];
  var coordinates;
  geocodeAddress(addresse, i); 

}

这篇关于将地理编码器结果保存到数组 - 关闭问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆