jquery-ui-map在点击事件上添加标记

时间:2012-10-13 08:44:23

标签: javascript jquery google-maps

目前我正在使用jquery-ui-map插件。我遇到了一个问题。是否可以通过此插件的方式添加一个事件监听器,该监听器将在用户点击它的地图上添加标记。在发现Api of this plugin之后我尝试了这段代码:

$('#map_canvas')
    .gmap()
    .addEventListener('click',function(event, test){
        //console.log(event);
    });

其中#map_canvas是包含我的地图的div元素。 此代码实际上添加了一个事件侦听器,但变量事件没有.latLng属性。我怎么能设法做到这一点?

2 个答案:

答案 0 :(得分:4)

Google-Fu帮助了我。这是example

$('#map_canvas').gmap().bind('init', function(event, map) { 
    $(map).click( function(event) {
        $('#map_canvas').gmap('addMarker', {
            'position': event.latLng, 
            'draggable': true, 
            'bounds': false
        }, function(map, marker) {
            //do whatever you need with the maker utilizing this variable
            marker.__gm_id
        });
    });
});

答案 1 :(得分:0)

尝试类似

的内容
$('#map_canvas').gmap().bind('init', function(ev, map) {
    $('#map_canvas').gmap('addMarker', {'position': '57.7973333,12.0502107', 'bounds': true}).click(function() {
        $('#map_canvas').gmap('openInfoWindow', {'content': 'Hello World!'}, this);
    });
})

了解更多参考资料check this link

相关问题