Google Maps API v3 - 单击标记时如何“提醒”标记ID [重复]
Posted
技术标签:
【中文标题】Google Maps API v3 - 单击标记时如何“提醒”标记ID [重复]【英文标题】:Google Maps API v3 - How to "alert" marker ID when marker is clicked [duplicate] 【发布时间】:2015-07-15 10:25:19 【问题描述】:这个问题很简单。使用此问题答案中的代码: Google Maps JS API v3 - Simple Multiple Marker Example 当它被点击时,我如何添加一个带有标记 id 的 javascript 警报(即:如果它是第一个标记,则为 0,如果它是第二个,则为 1,依此类推)?
基本上我需要为标记添加侦听器,当单击标记时,提醒标记的 ID。不知道还要澄清多少,所以它不会被标记为重复。
感谢您提供的任何帮助。
这是答案的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'),
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
);
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++)
marker = new google.maps.Marker(
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
);
google.maps.event.addListener(marker, 'click', (function(marker, i)
return function()
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
)(marker, i));
</script>
</body>
</html>
【问题讨论】:
【参考方案1】:将id:i
添加到for (i = 0; i < locations.length; i++)
循环中。现在您可以在infowindow.setContent
中使用marker.id
。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'),
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
);
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++)
marker = new google.maps.Marker(
id:i,
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
);
google.maps.event.addListener(marker, 'click', (function(marker, i)
return function()
infowindow.setContent(locations[i][0]+" ID="+marker.id);
infowindow.open(map, marker);
)(marker, i));
</script>
</body>
</html>
【讨论】:
正是我想要的。谢谢! i 也是在函数闭包中维护的,所以你可以照原样使用fiddle以上是关于Google Maps API v3 - 单击标记时如何“提醒”标记ID [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps API v3:在Firefox中未触发自定义标记的点击事件
如何从 Google Maps v3 API 中删除带有 mysql 数据的标记?
使用 jQuery mobile、google maps API v3 将 infoWindow 添加到地图标记
关闭 Google Maps API v3 中的所有信息窗口