javascript - 如何将 Google map 标记链接到其他元素?

标签 javascript google-maps

使用 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/

相关文章:

android - 不扩展 MapActivity 的 MapView

javascript - 删除对象数组中的谷歌地图标记

javascript - jquery中的简单数学

javascript - 创建 div 并向其添加背景图像

javascript - Amcharts 序列号 : How to get current zoom indexes

javascript - 除了react-native-background-task之外,还有什么方法可以在React Native中执行后台任务吗?

javascript - 动画 HTML、CSS JavaScript。我怎样才能实现这个

javascript - 谷歌地图 API : Adding letters to my markers on my google map

javascript - 无法使用谷歌地图标记,以及 javascript 全局变量的问题

android - 具有与 Google map 相同分辨率的 map 的 MapView