【发布时间】:2015-10-23 14:23:53
【问题描述】:
这是我的代码,基于 https://developers.google.com/maps/documentation/javascript/examples/event-simple 的示例,但我不想在单击标记时放大,而是想显示一个消息框 (alert()):
<!DOCTYPE html>
<html>
<head>
<title>Simple click event - modified</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var myLatlng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatlng
});
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Click to show alert'
});
marker.addListener('click', alert('Hello World!'));
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap" async defer>
</script>
</body>
</html>
当然,我没有填写 YOUR_API_KEY ,而是填写了我自己来自 Google Maps API v3 的 API 密钥。
每当我加载此页面时,该页面一加载就会显示警报,并且单击标记完全没有效果。
【问题讨论】:
标签: javascript google-maps google-maps-api-3 listener onclicklistener