最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I link Google Maps Markers to other elements? - Stack Overflow

programmeradmin2浏览0评论

Using the google maps (and JavaScript) I have been able to easily display several markers which each have a nice little info window over them.

//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);

However, I now want to be able to link the "click" of markers to functions which can update other parts of the page as well. For example, I would like to have a sidebar with copies of the marker infowindow content. The same way google maps shows results on the left and markers on the right. I might even want the click of sidebar content to open a given marker infowindow on the map.

The problem is that the GMarker click event only passes the lat/long - and I'm not sure how I can use that to find the matching div or whatever.

How do I get a unique id/handle for each marker?

Using the google maps (and JavaScript) I have been able to easily display several markers which each have a nice little info window over them.

//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);

However, I now want to be able to link the "click" of markers to functions which can update other parts of the page as well. For example, I would like to have a sidebar with copies of the marker infowindow content. The same way google maps shows results on the left and markers on the right. I might even want the click of sidebar content to open a given marker infowindow on the map.

The problem is that the GMarker click event only passes the lat/long - and I'm not sure how I can use that to find the matching div or whatever.

How do I get a unique id/handle for each marker?

Share Improve this question edited Oct 6, 2009 at 21:24 Chris B 15.9k5 gold badges35 silver badges40 bronze badges asked Oct 5, 2009 at 1:14 XeoncrossXeoncross 57.3k83 gold badges271 silver badges371 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Assign an id to each marker and its corresponding element in the sidebar. Create an event listener to call that id. Something like this:

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...
    });
}

See also this question.

For those interested there is a great example of linking dom elements with markers given here. Basically you just create a global JS array that holds a reference to each marker object.

发布评论

评论列表(0)

  1. 暂无评论