使用 google maps (和 JavaScript)我已经能够轻松地显示几个标记,每个标记上都有一个漂亮的小信息窗口。
//Create map over USA
map = new google.maps.Map2( document.getElementById('map') );
map.setCenter(new GLatLng(38.95940879245423, -100.283203125), 3);
//Create point, then marker, and then add to map
function create_marker(lat, lng, html) {
var marker = new GMarker( new GLatLng(lat,lng));
marker.bindInfoWindow(html);
map.addOverlay(marker);
}
var html = '<div>this is my text</div>';
create_marker(38.95940879245423, -100.283203125, html);
但是,我现在希望能够将标记的“点击”链接到也可以更新页面其他部分的函数。例如,我想要一个侧边栏,其中包含标记信息窗口内容的副本。同理google maps shows results在左边和右边的标记。我什至可能希望单击侧边栏内容以在 map 上打开给定的标记信息窗口。
问题是 GMarker单击事件仅传递纬度/经度 - 我不确定如何使用它来查找匹配的 div 或其他内容。
如何为每个标记获取唯一的 ID/句柄?
最佳答案
为侧边栏中的每个标记及其对应元素分配一个 id。创建一个事件监听器来调用该 ID。像这样:
var html = '<div>this is my text</div>';
var sideHtml = '<div id="1">this is my text</div>';
create_marker(38.95940879245423, -100.283203125, html, 1);
$("#sidebar").append(sideHtml); // Add the text to the sidebar (jQuery)
//Create point, then marker, and then add to map
function create_marker(lat, lng, html, id) {
var marker = new GMarker( new GLatLng(lat,lng));
marker.bindInfoWindow(html);
map.addOverlay(marker);
GEvent.addListener(marker, 'click', function(latlng) {
var div = document.getElementById(id); //access the sidebar element
// etc...
});
}
另见 this question .
关于javascript - 如何将 Google map 标记链接到其他元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1517870/