ジオコーディングを使用して配列からマップ マーカーを作成しようとしています。アドレスとアドレスのタイトルを配列に保存します。問題は私のループにあります。マーカーは配列内のアドレスから正しく設定されていますが、タイトルは最後の配列の最後の値に設定されています。ここに私のコードがあります:
maprender : function (comp, map) {
new google.maps.Marker({
animation: google.maps.Animation.DROP,
position: new google.maps.LatLng(this._geo.getLatitude(), this._geo.getLongitude()),
map: map
});
var names = new Array("ABC","DEF","GHI"),
mapAdd = new Array();
mapAdd[0] = "Address 1";
mapAdd[1] = "Address 2";
mapAdd[2] = "Address 3";
var geocoder = new google.maps.Geocoder();
for (var i = 0; i < mapAdd.length; i++) {
var lat = 0,
lng = 0,
x = names[i];
geocoder.geocode({
'address': mapAdd[i]},
function (results,status) {
if (status === google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat();
lng = results[0].geometry.location.lng();
console.log(lat + " " + lng);
new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
title: x,
map: map
});
console.log(x);
}
}
});
}
}
タイトルはすべてのマーカーで「GHI」を返しています。