如何在标记上添加一个监听器,单击标记时会显示“Hi”。我的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyAGcwOypxqebsKcF-A6pZUqDvijKbjltw0&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myLatlng = new google.maps.LatLng(61.455019, 23.84559);
var myOptions = {
zoom: 15,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.TERRAIN
}
var myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var myMarker = new google.maps.Marker({
position: myLatlng,
map: myMap,
});
google.maps.event.addListener(myMarker, 'click',
function()
{
var htmlString = "Hi";
myMarker.openInfoWindowHtml(htmlString);
}
);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
我想我在调用addListener事件时会出错。
答案 0 :(得分:2)
有关Google Maps API v3的documentation on infowindow,请参阅.openInfoWindowHtml方法。
答案 1 :(得分:0)
我认为问题可能出现在点击功能中......您可以从ur var myMarker中替换此代码。
var myMarker = new google.maps.Marker({
position: myLatlng,
map: myMap,
title: 'xxxxx'
});
google.maps.event.addListener(myMarker , 'click', function() {
var infowindow = new google.maps.InfoWindow;
infowindow.setContent('Hi');
infowindow.open(myMap, myMarker);
});