-1
中引用它的行。所以這是一個將被重構的函數的怪物,但現在我只是試圖讓它工作。谷歌地圖javascript:如果數組[i]是未定義的,忽略在infowindow.setContent
問題是,打開標記infowindow失敗時,它包含未定義的谷歌地方變量。我希望能夠忽略infowindow.setContent中引用未定義變量的行,以便無論Google地方信息可用,infowindow都會打開。我可以單獨檢查每個人,但希望有人能找到更有說服力的解決方案。
我的代碼:
//passed a google place object
function formatInfoWindow(place) {
var name = place.name,
open = place.opening_hours.open_now,
address = place.vicinity,
phoneI = place.international_phone_number,
phone = place.formatted_phone_number
rating = place.rating,
reviewAuth = place.reviews[0].author_name,
reviewText = place.reviews[0].text,
reviewRate = place.reviews[0].rating,
website = place.website,
img = place.photos[0],
photo = img.getUrl(),
info = this.info;
//creates an array of all the place variables
var infoWindowArray = [name, open, address,phoneI, phone, rating, reviewAuth, reviewText, reviewRate, website, img, photo, info];
//loop through array to find undefined variables
for (var i = 0; i < infoWindowArray.length; i++) {
if (typeof infoWindowArray[i] === 'undefined') {
//somehow ignore those variables in setContent block
}
}
this.infowindow.setContent(
'<div class="infowindow"><strong><h1>' + name + '</h1></strong>' +
'<address>' + address + '</address>'+
'<p>Open Now: ' + open + ' Rating: ' + rating + ' ' + '<a href="' + website + '">Website</a></p>'+
'<p><a href="tel:' + phoneI + '">'+phone+'</a></p>'+
'<h3><b>Notes:</b></h3>'+
'<p>' + info + '</p>'+
'<h4>' + reviewAuth + ' Rating: '+reviewRate+'</h4>'+
'<p>' + reviewText + '</p>'+
'<img src='+ photo + ' ></img>'+
'</div>'
);
this.infowindow.open(map, this);
}
謝謝!我試圖使用模板字符串,但解決方案不斷變得複雜,所以我放棄了它。太棒了。 –
你能解釋一下這個函數嗎(match,prop){ return infos [prop] || 「」; }); –
更多信息在這裏:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – Diode