谷歌地图事件
Google Maps Events
我有一个谷歌地图在我的项目与多个标记。我的目标是当我点击标记时,执行一个函数(toggleBounce)。所以我将每个标记添加到google。maps。event中。addListener,像这样:
for(var i in markers){
google.maps.event.addListener(marker[i], 'click', toggleBounce);
}
好好工作!所以我的问题是在函数toggleBounce中,我如何传递标记的数量?
function toggleBounce() {
if (marker[WHAT I NEED TO PUT HERE?].getAnimation() != null) {
marker[WHAT I NEED TO PUT HERE?].setAnimation(null);
}
else {
marker[WHAT I NEED TO PUT HERE?].setAnimation(google.maps.Animation.BOUNCE);
}
}
在事件处理程序事件中使用this
(它是标记):
function toggleBounce() {
if (this.getAnimation() != null) {
this.setAnimation(null);
}
else {
this.setAnimation(google.maps.Animation.BOUNCE);
}
}
概念验证
代码片段:
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]
];
function initMap() {
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', toggleBounce);
}
}
google.maps.event.addDomListener(window, 'load', initMap);
function toggleBounce() {
if (this.getAnimation() != null) {
this.setAnimation(null);
} else {
this.setAnimation(google.maps.Animation.BOUNCE);
}
}
html,
body,
#map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
我创建了一个活塞,它可能会帮助你。
http://plnkr.co/edit/4hFdmxji9C74ymBL1qtS?p =
预览var createMarker = function (info){
var marker = new google.maps.Marker({
map: scope.map,
position: new google.maps.LatLng(info.lat, info.lon),
title: info.place
});
google.maps.event.addListener(marker, 'click', function(){
//do your stuff
if (marker.getAnimation() != null) {
marker.setAnimation(null);
}
else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
});
}
for (var i = 0; i < items.length; i++){
items[i].id=i+'AM@_255';
createMarker(items[i]);
}
相关文章:
- 在谷歌地图上获取事件的x,y坐标
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- 当谷歌地图进入街景时,我能抓住什么事件
- 谷歌地图动态创建的标记点击事件使用相同的标记
- 如何在多承诺链中处理谷歌地图API V3事件
- 将谷歌地图点击事件中的LatLng传递到rails控制器
- 在JQuery中使用谷歌地图Api事件/侦听器
- 点击事件在谷歌地图和AJAX
- 信息窗口谷歌地图点击事件给出错误
- firefox(谷歌地图)中未触发的点击事件
- 谷歌地图API-点击事件仅在边缘点击时触发
- 加载自定义磁贴后的谷歌地图事件
- 谷歌地图Javascript v3多段线点击事件
- 谷歌地图点击事件移动标记
- 谷歌地图API v3调整大小事件
- 如果谷歌地图绘图(如onDraw)触发什么事件
- 重叠圆圈阻止鼠标悬停事件-谷歌地图v3
- 显示谷歌地图点击事件 - 谷歌地图API
- 避免在地图平移时自定义叠加点击事件(谷歌地图 API)
- 简单点击事件谷歌地图