将onclick事件添加到google.map标记

时间:2013-06-07 13:55:20

标签: javascript api google-maps

我一直试图弄清楚JS的一点点:(我有谷歌地图

var myCenter=new google.maps.LatLng(53, -1.33);

function initialize()
{
var mapProp = {
    center:myCenter,
    zoom: 14,
    draggable: false,
    scrollwheel: false,
    mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);

var marker=new google.maps.Marker({
    position:myCenter,
    icon:'images/pin.png',
    url: 'http://www.google.com/',
    animation:google.maps.Animation.DROP
});
marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

但我似乎无法连接标记网址的onclick事件?

我知道它与添加

有关
google.maps.event.addListener(marker, 'click', function() {window.location.href = marker.url;});

但是我把它放在哪里会导致地图无法显示或标记不显示。

6 个答案:

答案 0 :(得分:18)

确保在initialize()之外定义marker。否则,如果您尝试将click监听器分配到initialize()之外,它将为undefined

此外,如果您尝试加载网址www.google.com,则可能会出现SAME-ORIGIN问题,但它应该可以正常使用本地网址。

更新了代码

var myCenter=new google.maps.LatLng(53, -1.33);

var marker=new google.maps.Marker({
    position:myCenter,
    url: '/',
    animation:google.maps.Animation.DROP
});

function initialize()
{
var mapProp = {
    center:myCenter,
    zoom: 14,
    draggable: false,
    scrollwheel: false,
    mapTypeId:google.maps.MapTypeId.ROADMAP
};

var map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);

marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addListener(marker, 'click', function() {window.location.href = marker.url;});

Working Demo on Bootply

答案 1 :(得分:5)

这是我使用的:

var latLng = new google.maps.LatLng(53, -1.33);

var map = new google.maps.Map(document.getElementById('map_canvas'), {
    center: latLng,
    draggable: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    scrollwheel: false,
    zoom: 14
});

var marker = new google.maps.Marker({
    animation: google.maps.Animation.DROP,
    icon: 'images/pin.png',
    map: map,
    position: latLng
});

google.maps.event.addDomListener(marker, 'click', function() {
    window.location.href = 'http://www.google.co.uk/';
});

我不确定您是否可以使用url对象存储Marker属性。

如果您需要显示多个标记(即来自API调用),那么您可以像这样使用for循环:

for (var i = 0; i < markers.length; i++) {
    (function(marker) {
        var marker = new google.maps.Marker({
            animation: google.maps.Animation.DROP,
            icon: 'images/pin.png',
            map: map,
            position: market.latLng
        });

        google.maps.event.addDomListener(marker, 'click', function() {
            window.location.href = marker.url;
        });
    })(markers[i]);
}

答案 2 :(得分:0)

这是我过去所做的。我在代码和你的代码之间看到的唯一区别是我在标记选项中设置了标记贴图,并且您使用marker.setMap(Map)设置;

var marker = new window.google.maps.Marker({
        position: markerPosition,
        map: map,
        draggable: false,
        zIndex: zIndex,
        icon: getNewIcon(markerType != "archive", isBig)
        , animation: markerAnimation
    });


    window.google.maps.event.addListener(marker, 'click', function () {
        // do stuff
    });

答案 3 :(得分:0)

不确定您的内容在哪里,但您需要执行以下操作...

var yourContent = new google.maps.InfoWindow({
    content: 'blah blah'
});

google.maps.event.addListener(marker, 'click', function() {
  yourContent.open(map,marker);
});

答案 4 :(得分:0)

var myLatlng = new google.maps.LatLng(-25.363882, 131.044922);
var mapOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: 'Hello World!',
    url: 'http://www.google.com'
});
google.maps.event.addListener(marker, 'click', function () {
    window.location = marker.url;
});

如果您转到此页面:https://google-developers.appspot.com/maps/documentation/javascript/examples/full/marker-simple

并将上面的代码粘贴到控制台中,您将看到它的工作原理。

我认为你遇到的问题如下,你需要把这一行:

google.maps.event.addListener(marker, 'click', function () {
    window.location = marker.url;
});

在初始化函数内部。

答案 5 :(得分:0)

就我而言,我有多个标记,我想知道单击了哪个标记。我在Google论坛上找到了以下解决方案-https://groups.google.com/g/google-maps-js-api-v3/c/cAJrWZWdD-8?pli=1

由Chris Broadfoot(Google员工)

您可以使用'this'引用点击的标记:

google.maps.event.addListener(marker,'click',markerListener);

function markerListener(){alert(this.getPosition()); // this.setIcon(...}