Я читать, читать и читать еще немного, и в результате я придумал следующее:Google Maps Геокодировать Ответный Параметр ошибки
// Place Markers on the Map
var PlaceMarkers = function (iw, adds, gc) {
var image = {url: "http://mywebstte.com/Images/star2.png", size: new google.maps.Size(24, 24)};
var aCt = adds.length;
for(var i = 0; i < aCt; ++i) {
GetLatLng(gc, adds[i].address, function(pos){
if(pos){
var ipop = '<h1>' + adds[i].title + '</h1>';
if(!isBlank(adds[i].url)){
ipop += '<a href="' + adds[i].url + '" target="_blank">' + adds[i].url + '</a><br />';
}
ipop += '<div class="map_item_content" id="mi_content' + i + '">' + adds[i].content + '</div>';
if(!isBlank(adds[i].mainphone)){
ipop += '<br /><strong>Phone:</strong> <a href="tel:'+adds[i].mainphone+'">' + adds[i].mainphone + '</a>';
}
if(!isBlank(adds[i].mainemail)){
ipop += '<br /><strong>Email:</strong> <a href="mailto:'+adds[i].mainemail+'">' + adds[i].mainemail + '</a>';
}
console.log('HEY NOW: ' + pos.toString() + ' - Location Found!'); // Never displays, as a result neither do the markers
var mark = new google.maps.Marker({title: adds[i].title, position: pos, map: map, icon: image, html: ipop});
google.maps.event.addListener(mark, 'click', function(){
iw.setContent(this.html);
iw.open(map, this);
});
}
});
}
};
// Get Lat/Lng Location
var GetLatLng = function(gc, add, callback) {
var ret = '';
gc.geocode({'address': add}, function(res, status) {
if (status == 'OK') {
ret = res[0].geometry.location;
console.log('Found Here: ' + ret.toString()); // Always displays
} else {
ret = null;
console.log('Nothing Found For: ' + add);
}
});
callback(ret);
};
Теперь в GetLatLng
, моя консоль покажет найденный lat/lng для адреса. Я прочитал, что мне нужен обратный вызов, потому что эти вызовы асинхронны, однако даже после использования обратного вызова PlaceMarkers
по-прежнему остается пустым и не помещает маркеры.
EDIT Для GetLatLng
// Get Lat/Lng Location
var GetLatLng = function(gc, add, f) {
var ret = '';
gc.geocode({'address': add}, function(res, status) {
if (status == 'OK') {
f(res[0].geometry.location);
console.log('Found Here: ' + ret.toString()); // Always displays
}
});
return -1;
};
Теперь возвращается, но я получаю сообщение об ошибке с TypeError: adds[i] is undefined
Возможный дубликат [Почему моя переменная неизменна после того, как я изменяю ее внутри функции? - Асинхронная ссылка на код] (http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – geocodezip