javascript - 谷歌地图 API : open url by clicking on marker

标签 javascript google-maps google-maps-api-3 google-maps-markers

我想使用 Google Maps API 3 通过点击标记打开一个新窗口。

不幸的是,Google Maps API 的示例并不多,我发现了这段代码:

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

当我用循环创建标记时如何使用它?我以多种方式尝试过,但负担不起。

这是我的代码——我让它变得简单而简短:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

    <style type="text/css">
        html { height: 100% }
        body { height: 100%; margin: 0; padding: 0 }
        #map_canvas { height: 100% }
    </style>

    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">
    var points = [
        ['name1', 59.9362384705039, 30.19232525792222, 12],
        ['name2', 59.941412822085645, 30.263564729357767, 11],
        ['name3', 59.939177197629455, 30.273554411974955, 10]
    ];

    function setMarkers(map, locations) {
        var shape = {
            coord: [1, 1, 1, 20, 18, 20, 18 , 1],
            type: 'poly'
        };

        for (var i = 0; i < locations.length; i++) {
            var flag = new google.maps.MarkerImage('markers/' + (i + 1) + '.png',
            new google.maps.Size(17, 19),
            new google.maps.Point(0,0),
            new google.maps.Point(0, 19));

            var place = locations[i];
            var myLatLng = new google.maps.LatLng(place[1], place[2]);
            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                icon: flag,
                shape: shape,
                title: place[0],
                zIndex: place[3]
            });
        }
    }

    function initialize() {
        var myOptions = {
            center: new google.maps.LatLng(59.91823239768787, 30.243222856188822),
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
        setMarkers(map, points);
    }
    </script>
</head>

<body onload="initialize()">
    <div id="map_canvas" style="width:50%; height:50%"></div>
</body>
</html>

如何用上面的代码点击标记打开url?

最佳答案

您可以为每个点添加一个特定的 url,例如:

var points = [
    ['name1', 59.9362384705039, 30.19232525792222, 12, 'www.google.com'],
    ['name2', 59.941412822085645, 30.263564729357767, 11, 'www.amazon.com'],
    ['name3', 59.939177197629455, 30.273554411974955, 10, 'www.stackoverflow.com']
];

将 url 添加到 for 循环中的标记值:

var marker = new google.maps.Marker({
    ...
    zIndex: place[3],
    url: place[4]
});

然后您可以在 for 循环的末尾之前添加:

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

另见 example .

关于javascript - 谷歌地图 API : open url by clicking on marker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8769966/

相关文章:

javascript - map 旋转时如何找到 Google Map V3 的 4 个 Angular ?

google-maps - Delphi 的谷歌地图库突然停止工作?

javascript - RadWindow 未在客户端上打开

javascript - 等待慢速 JSF 页面的消息

android - 在 Linux fedora 上获取 Google map key 时,我得到 "bash: keytool: command not found."

javascript - 从 mysql 刷新谷歌地图标记

javascript - MDL jQuery : label overlaps the content into a input field

javascript - Extjs添加到组合框不可选择的="on"属性

jQuery 谷歌地图问题

javascript - 如何在 Angular 2 中使用谷歌地图