我已经阅读,阅读和阅读了更多内容,因此我提出了以下建议:
// 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
之后仍然是空白的,也不会放置标记。
编辑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
答案 0 :(得分:2)
您需要使用地理编码器回调中可用的数据:
// 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);
} /* end of geocoder callback function */ );
};