我正在尝试创建一个带有信息窗口的 map ,该信息窗口通过用户单击标记来打开。到目前为止,代码似乎不起作用, map 之前显示过,但是一旦我输入信息窗口弹出窗口的代码,不仅信息窗口没有显示,而且 map 也停止在我的网页上显示。代码如下:
<div id= "map"> </div>
<script>
var map;
var markers=[];
function initMap(){
map= new google.maps.Map(document.getElementById('map'),{
center:{ lat:40.7413549, lng:-73.9980244},
zoom:13
});
var locations=[
{title:"Park Ave Penthouse",location:{lat:40.7713024,lng:-73.9632393}},
{title:"Chelsea Loft", location;{lat:40.7444883,lng:-73.9949465}},
{title:"Union Square Open Floor Plan",location:{lat:40.7347062,lng:-73.984377}},
{title:"East Village Hip Studio",location:{lat:40.7281777,lng:-73.984377}},
{title:"TriBeCa Artsy Bachelor Pad",location:{lat:40.7195264,lng:-74.0089934}},
{title:"Chinatown Homey Space",location:{lat:40.7180628,lng:-73.9961237}}
];
var largeInfowindow= new google.maps.InfoWindow();
var bounds= new google.maps.LatLngBounds();
for (var i=0; i< locations.lenght;i++){
var position = locations[i].location;
var title = locations[i].title;
var marker= new google.maps.Marker({
map: map,
position:position,
title:title,
animation:google.maps.Animation.Drop,
id:i
});
markers.push(marker);
bounds.extend(marker.position);
marker.addListner ("click",function(){
populateInfoWindow(this,largeInfowindow);
});
map.fitBounds(bounds);
};
markers.push(marker);
markers.addListner("click",function(){
populateInfoWindow(this,largeInfoWindow);
});
}
function populateInfoWindow (marker, infowindow){
if(infowindow.marker=!marker){
infowindow.marker= marker;
infowindow.setContent("<div>"+marker.title+"</div>");
infowindow.open(map,marker);
infowindow.addListner("closeclick",function(){
infowindow.setMarker(null);
});
}
}
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvrWT4KKcsDSSGYXpxRry3m7j90pPjZgA&v=3&callback=initMap"></script>
最佳答案
错误在这一行:
if(infowindow.marker=!marker){ //it is "!=" and not "=!"
始终检查控制台是否有 JavaScript 错误。在 infowindow 代码之后,您甚至看不到 mao,这意味着您有 javascript 错误。
关于javascript - 带有 infowindow 的 Google map api 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971230/