问题描述
好吧,所以我有一段时间寻找这个问题的解决方案,但我没有发现任何具体的。
在您指向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的Geocoder将地址的纬度和经度保存到数组中。我设法正确计算所有的值,但我只是不能把它保存到数组。我已经使用一个匿名函数传递地址到函数,但保存仍然不工作。请帮助!
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地图。我知道,这将是更方便的只是创建地图,但由于其他原因是不可能的。
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 addressesThis 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;
}
推荐答案
地理编码是异步的。
类似(未测试)
更新为使用函数闭包
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);
}
这篇关于将地理编码器结果保存到数组 - Closure Trouble的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!