我正在尝试使用用户点击标记打开的信息窗口创建地图。到目前为止代码似乎没有工作,地图显示之前,但一旦我输入信息窗口弹出窗口的代码不仅信息窗口没有显示,但地图也停止显示在我的网页上。这是代码:
<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>
答案 0 :(得分:1)
错误在这一行:
if(infowindow.marker=!marker){ //it is "!=" and not "=!"
始终检查控制台是否存在javascript错误。在infowindow代码之后,你甚至看不到毛,这意味着你有javascript错误。