[英]Show InfoWindow when clicking on marker
I would like to use an info window for my map.我想为我的地图使用信息窗口。
When I run this code, the info window is not appearing.当我运行此代码时,信息窗口没有出现。 I want it to appear when I click on the marker.
我希望它在我点击标记时出现。
<div id="map" style="width:100%;height:320px"></div>
<script>
function myMap() {
var mapCanvas = document.getElementById("map");
var mapOptions = {
center: new google.maps.LatLng(47.3921611, 8.4957963),
zoom: 14
}
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335 km (208 mi) '+
'south west of the nearest large town, Alice Springs; 450 km '+
'(280 mi) by road. Kata Tjuta and Uluru are the two major '+
'features of the Uluru - Kata Tjuta National Park. Uluru is '+
'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
'Aboriginal people of the area. It has many springs, waterholes, '+
'rock caves and ancient paintings. Uluru is listed as a World '+
'Heritage Site.</p>'+
'<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var map = new google.maps.Map(mapCanvas, mapOptions);
var image = '<?php echo bloginfo("url") ?>/G_marker.png';
var beachMarker = new google.maps.Marker({
position: map.getCenter(),
map: map,
icon: image,
});
}
marker.addListener('click', function() {
infowindow.open(map, marker);
});
google.maps.event.addDomListener(window, 'load', myMap);
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCTzoFPuiNSd_JH_jGT7Pq_KMM7XoV2PMM&callback=myMap"></script>
I actually copied the InfoWindow part and pasted it to my code but I still don't understand why it is not working.我实际上复制了 InfoWindow 部分并将其粘贴到我的代码中,但我仍然不明白为什么它不起作用。 How can I fix this?
我怎样才能解决这个问题?
I get a javascript error with the posted code: Uncaught ReferenceError: marker is not defined
.我收到一个带有发布代码的 javascript 错误:
Uncaught ReferenceError: marker is not defined
。 Your code calls that variable beachMarker
, and the click listener should be set up inside the myMap
(initialize) function (in the scope where that variable exists).代码调用该变量
beachMarker
,然后单击监听器应设置里面myMap
(初始化)功能(在该变量存在的范围)。
code snippet:代码片段:
html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px }
<div id="map" style="width:100%;height:320px"></div> <script> function myMap() { var mapCanvas = document.getElementById("map"); var mapOptions = { center: new google.maps.LatLng(47.3921611, 8.4957963), zoom: 14 } var contentString = '<div id="content">' + '<div id="siteNotice">' + '</div>' + '<h1 id="firstHeading" class="firstHeading">Uluru</h1>' + '<div id="bodyContent">' + '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' + 'sandstone rock formation in the southern part of the ' + 'Northern Territory, central Australia. It lies 335 km (208 mi) ' + 'south west of the nearest large town, Alice Springs; 450 km ' + '(280 mi) by road. Kata Tjuta and Uluru are the two major ' + 'features of the Uluru - Kata Tjuta National Park. Uluru is ' + 'sacred to the Pitjantjatjara and Yankunytjatjara, the ' + 'Aboriginal people of the area. It has many springs, waterholes, ' + 'rock caves and ancient paintings. Uluru is listed as a World ' + 'Heritage Site.</p>' + '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">' + 'https://en.wikipedia.org/w/index.php?title=Uluru</a> ' + '(last visited June 22, 2009).</p>' + '</div>' + '</div>'; var infowindow = new google.maps.InfoWindow({ content: contentString }); var map = new google.maps.Map(mapCanvas, mapOptions); var image = 'http://maps.google.com/mapfiles/ms/micons/blue.png'; var beachMarker = new google.maps.Marker({ position: map.getCenter(), map: map, icon: image, }); beachMarker.addListener('click', function() { infowindow.open(map, beachMarker); }); } </script> <script src="https://maps.googleapis.com/maps/api/js?callback=myMap"></script>
You created "beachMarker" variable and then uses "marker" :P您创建了“beachMarker”变量,然后使用“marker”:P
Try use:尝试使用:
beachMarker.addListener('click', function() {
infowindow.open(map, beachMarker);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.