必须有一种方法可以为ALL MARKERS添加一个监听器,目前我正在使用一个感觉非常错误的循环为每个监听器添加一个监听器......
这感觉不对:
google.maps.event.addListener(unique_marker_id, 'click', function(){
//do something with this marker...
});
答案 0 :(得分:39)
在Marker
和MarkerWithLabel
两种情况下,您也可以使用 this 关键字来引用事件处理程序所针对的对象附上:
google.maps.event.addListener(marker, 'click', function () {
// do something with this marker ...
this.setTitle('I am clicked');
});
此 这里指的是特定的标记对象。
答案 1 :(得分:30)
您需要将侦听器添加到每个标记,但是您可以通过例如定义像
这样的函数function createMarker(pos, t) {
var marker = new google.maps.Marker({
position: pos,
map: m, // google.maps.Map
title: t
});
google.maps.event.addListener(marker, 'click', function() {
alert("I am marker " + marker.title);
});
return marker;
}
并适当地调用它:
var m1 = createMarker(new google.maps.LatLng(...), "m1");
var m2 = createMarker(new google.maps.LatLng(...), "m2");
或循环等
答案 2 :(得分:4)
我设法使用FusionTablesLayer完成了这项工作。这是一些正确设置一切的工作,但是一旦完成,它就会超快,即使有数千个标记。
您基本上可以在Google文档中创建公开表格,并从您的网页进行查询。该地图是在Googles的服务器上预先生成的,这就是它表现良好的原因。
完整的演示页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Google Maps Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
html, body, #map_canvas
{
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize()
{
var denmark = new google.maps.LatLng(56.010666, 10.936890);
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: denmark,
zoom: 7,
mapTypeId: 'roadmap'
});
layer = new google.maps.FusionTablesLayer({
query: {
select: 'Coordinates',
from: '1234567'
}
});
layer.setMap(map);
google.maps.event.addListener(layer, 'click', function (event) {
alert('Hello World!'); });
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
</body>
</html>
查看此文章了解更多信息,来自Google Geo API小组的Luke Mahe和Chris Broadfoot的"Too Many Markers!"。
答案 3 :(得分:4)
如果您使用的是GoogleMaps v3.16或更高版本,则可以将事件处理程序添加到整个map.data
图层。
var map = new google.maps.Map(document.getElementById("map-canvas"), options);
map.data.loadGeoJson('http://yourserver.com/path/to/geojson.json');
map.data.addListener('click', function(e) {
// this - instance of the layer object, in this case - map.data
// e.feature - instance of the feature object that initiated the event
// e.latLng - the position of the event
});
请参阅:https://developers.google.com/maps/documentation/javascript/examples/layer-data-event
答案 4 :(得分:0)
我设法得到了答案: Google Maps and Their Markers
答案 5 :(得分:0)
这种最简单的方法是:
google.maps.event.addListener(marker, 'click', function() {
var marker = this;
alert("Tite for this marker is:" + this.title);
});
答案 6 :(得分:0)
为了那些想要添加自定义标签等的搜索,请扩展Jiri回答purley。以Jiri文章的精神,简写版本:
var m1 = createMarker({lat: -25.363, lng: 131.044}, "m1", "<div id=\"content\"><div id=\"siteNotice\"></div> <h1 id=\"firstHeading\" class=\"firstHeading\">m1</h1> <div id=\"bodyContent\">Some info</div> </div>");
function createMarker(pos, t, d) {
var marker = new google.maps.Marker({
position: pos,
map: map,
title: t
});
google.maps.event.addListener(marker,"click", function() {
alert("I am marker " + marker.title);
new google.maps.InfoWindow({ content: d }).open(map, marker);
});
return marker;
}
删除警报,只是在那里显示动作等,就像Jiri的信息一样,你可以添加m2,m3等等。我认为这简单地完成了任务。
答案 7 :(得分:0)
public void OpenNotification(String key) {
String keys[] = { key };
StatusBarNotification sbns[] = getActiveNotifications(keys);
for (StatusBarNotification sbn : sbns) {
try {
if (sbn == null) {
Log.i(TAG, "sbn is null");
continue;
}
Notification n = sbn.getNotification();
if (n.contentIntent != null) {
PendingIntent pi = n.contentIntent;
if (pi != null) {
pi.send();
}
}
} catch (Exception e) {
}
}
}
答案 8 :(得分:0)
我所做的是,在地图上添加新的marker
并将其推入markers = []
数组之前,我只是向其添加了marker.addListener('click', () => { // Do Something here});
监听器
完整代码:
// Adds a marker to the map and push to the array.
function addMarker(location) {
var marker = new google.maps.Marker({
position: location,
icon: {
url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
},
animation: google.maps.Animation.DROP,
map: map
});
marker.addListener("click",
() => {
console.log("Marker Click Event Fired!");
});
markers.push(marker);
}
LINK我关注了!
我真的很难找到满足自己需求的具体答案。但是,这个对于我来说现在无处不在! 问候! :)
答案 9 :(得分:-1)
var map;
function initialize_map(locations) {
var options = {
zoom: 8,
center: new google.maps.LatLng(59.933688,30.331879),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), options);
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][lat], locations[i][lng]),
map: map,
title: locations[i][title]
});
set_event(marker);
bounds.extend(marker.position);
}
map.fitBounds(bounds);
}
function set_event(marker) {
google.maps.event.addListener(marker, 'click', function() {
// do something with this marker ...
});
}
答案 10 :(得分:-1)
您可以这样做:
function setMarkers(map, locations) {
var image = ['circle_orange.png','circle_blue .png'];
for (var i = 0; i < locations.length; i++) {
var stations = locations[i];
var myLatLng = new google.maps.LatLng(stations[1], stations[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image[stations[3]],
title: stations[0],
zIndex: stations[3],
optimized: false
});
var infowindow = new google.maps.InfoWindow({
content: "No data available"
});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.setContent("We can include any station information, for example: Lat/Long: "+ stations[1]+" , "+stations[2]);
infowindow.open(map, this);
});
}
}