我正在尝试输出数组中每个地址的lat和long。
请参阅小提琴:http://jsbin.com/simumo/5/edit?html,js,console,output
JavaScript的:
var address = [ "Collins+Street,+MELBOURNE,+VIC,+AUSTRALIA,+3000", "Sussex+Street,+SYDNEY,+NSW,+AUSTRALIA,+2000", "George+Street,+BRISBANE,+QLD,+AUSTRALIA,+4000"];
jQuery.each(address, function(index, item) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
console.log(latitude, longitude);
}
});
我认为我的迭代有点搞砸了。我的最终目标是将这些显示为列表,但我暂时会在控制台中找到它。
答案 0 :(得分:2)
有一些语法错误,这是你的最终代码应该是什么样的:
var address = [ "Collins+Street,+MELBOURNE,+VIC,+AUSTRALIA,+3000", "Sussex+Street,+SYDNEY,+NSW,+AUSTRALIA,+2000", "George+Street,+BRISBANE,+QLD,+AUSTRALIA,+4000"];
jQuery.each(address, function(index, item) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': item}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
console.log(latitude, longitude);
}
});
});
还要注意我是如何改变的
{'address': address}
到
{'address':item}
因为我们现在引用数组本身的项和索引!
这是一个有效的演示:http://jsbin.com/vujuwoco/1/edit?html,js,console,output