Google Map API v3 InfoWindow 无法使用函数中的 setContent 打开
我有一个简单的 Google 地图 v3 代码。它创建地图并将标记添加到markerCluster。除了将内容设置到 infoWindow 并打开它之外,一切都工作正常。 函数getSupplierDetails()
仅返回一个短字符串(即“Red Supply”)。
问题是:如果我将文本“Red Supply”硬编码到 setContent 行,例如 infoWindow.setContent("Red Supply");
,则信息窗口可以正常打开内容。
但是,如果我像下面的代码一样保留它,则信息窗口根本不会打开,尽管 getSupplierDetails()
函数返回“Red Supply”。
getSupplierDetails()
函数从 Firebug 返回此 JSON 字符串:{"popupContent": "Red Suppplier"}
。
搞了这么久也没有解决。任何帮助表示赞赏。
谢谢
var map;
var markerCluster;
var infoWindow;
$(document).ready(function() {
if (mapElem != null) {
var latlng = new google.maps.LatLng(54.664936, -2.706299);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(mapElem, myOptions);
markerCluster = new MarkerClusterer(map);
infoWindow = new google.maps.InfoWindow();
AddMarkers();
}
});
function AddMarkers(){
markerCluster.clearMarkers();
var marker = new google.maps.marker({position:latLng, map:map, title:"title"});
google.maps.event.addListener(marker, 'click', function() {
var res = getSupplierDetails();
infoWindow.setContent(res);
infoWindow.open(map, this);
});
markers.push(marker);
markerCluster.addMarkers(markers);
}
function getSupplierDetails() { //returns {"popupContent": "Red Suppplier"}
$.ajax({
type: 'POST',
url: "sitedetail.aspx",
dataType: 'json',
timeout: 30000,
success: function(data) {
return data.popupContent;
},
error: function(xhr, textStatus, thrownError) {
var resp = JSON.parse(xhr.responseText);
alert(resp.message);
}
});
}
I have a simple Google map v3 code. It creates the map and adds a marker to the markerCluster. It is all working fine, except for setting content to the infoWindow and opening it.
The function getSupplierDetails()
simply returns a short string (i.e. "Red Supply").
Here is the problem: If I hard code the text "Red Supply" to the setContent line like infoWindow.setContent("Red Supply");
then the infowindow open fine with the content.
But if I leave it as it is as in the code below the infowindow does not open at all, although the getSupplierDetails()
function returns "Red Supply".
getSupplierDetails()
function returns this JSON string: {"popupContent": "Red Suppplier"}
from Firebug.
Spent so long on it without any solution. Any help appreciated.
Thanks
var map;
var markerCluster;
var infoWindow;
$(document).ready(function() {
if (mapElem != null) {
var latlng = new google.maps.LatLng(54.664936, -2.706299);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(mapElem, myOptions);
markerCluster = new MarkerClusterer(map);
infoWindow = new google.maps.InfoWindow();
AddMarkers();
}
});
function AddMarkers(){
markerCluster.clearMarkers();
var marker = new google.maps.marker({position:latLng, map:map, title:"title"});
google.maps.event.addListener(marker, 'click', function() {
var res = getSupplierDetails();
infoWindow.setContent(res);
infoWindow.open(map, this);
});
markers.push(marker);
markerCluster.addMarkers(markers);
}
function getSupplierDetails() { //returns {"popupContent": "Red Suppplier"}
$.ajax({
type: 'POST',
url: "sitedetail.aspx",
dataType: 'json',
timeout: 30000,
success: function(data) {
return data.popupContent;
},
error: function(xhr, textStatus, thrownError) {
var resp = JSON.parse(xhr.responseText);
alert(resp.message);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ajax 成功调用是异步的,这就是为什么你得到 null,你所做的很好(将 SetContent 移动到 de success 部分),但你也可以这样做
The ajax success call is asynchronous that's why you are getting null, What you did is fine (moving the SetContent to de success part) but you could also do this