javascript - 如何向谷歌地图标记添加唯一链接

标签 javascript api google-maps

我只是想弄清楚如何向每个 Google map 标记添加唯一链接。

我尝试向每个位置数组项添加第三个值,然后将 url:locations[2] 添加到新的谷歌地图类,但它只是破坏了 map 。

我有以下 JS...

            function initMap() {

            var centerPoint = { lat: 51.6856885, lng: -3.6304398 };

            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 10,
                center: centerPoint,
                styles: [{ "featureType": "all", "elementType": "labels.text.fill", "stylers": [{ "color": "#ffffff" }] }, { "featureType": "all", "elementType": "labels.text.stroke", "stylers": [{ "visibility": "on" }, { "color": "#424b5b" }, { "weight": 2 }, { "gamma": "1" }] }, { "featureType": "all", "elementType": "labels.icon", "stylers": [{ "visibility": "off" }] }, { "featureType": "administrative", "elementType": "geometry", "stylers": [{ "weight": 0.6 }, { "color": "#545b6b" }, { "gamma": "0" }] }, { "featureType": "landscape", "elementType": "geometry", "stylers": [{ "color": "#545b6b" }, { "gamma": "1" }, { "weight": "10" }] }, { "featureType": "poi", "elementType": "geometry", "stylers": [{ "color": "#666c7b" }] }, { "featureType": "poi.park", "elementType": "geometry", "stylers": [{ "color": "#545b6b" }] }, { "featureType": "road", "elementType": "geometry", "stylers": [{ "color": "#424a5b" }, { "lightness": "0" }] }, { "featureType": "transit", "elementType": "geometry", "stylers": [{ "color": "#666c7b" }] }, { "featureType": "water", "elementType": "geometry", "stylers": [{ "color": "#2e3546" }] }]
            });

            // Create an array of alphabetical characters used to label the markers.
            var labels = '';

            // Add some markers to the map.
            // Note: The code uses the JavaScript Array.prototype.map() method to
            // create an array of markers based on a given "locations" array.
            // The map() method here has nothing to do with the Google Maps API.
            var markers = locations.map(function (location, i) {
                return new google.maps.Marker({
                    position: location,
                    label: labels[i % labels.length]
                });
            });

            // Add a marker clusterer to manage the markers.
            var markerCluster = new MarkerClusterer(map, markers,
                { imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m' });
        }
        var locations = [
          { lat: 51.5474973, lng: -3.5777395 },
          { lat: 51.4668561, lng: -3.1689607 },
          { lat: 51.6156059, lng: -3.6934229 },
          { lat: 51.5250486, lng: -3.6780919 },

          { lat: 51.6428655, lng: -3.9859831 },
          { lat: 51.5447132, lng: -3.5961878 },
          { lat: 51.5678233, lng: -3.2866061 },
          { lat: 51.5122641, lng: -3.5073934 },
          { lat: 51.4668561, lng: -3.1689607 }
        ]

最佳答案

如果我正确理解你的问题。您可以将第三个元素添加到您的位置数组项中,但您无法按原样直接使用它。你可以有

var locations = [
      { latLng:{lat: 51.5474973, lng: -3.5777395},url:'google.com' }
]

然后在你的函数中执行此操作

return new google.maps.Marker({
    position: location.latLng,
    url: location.url,
    label: labels[i % labels.length]
});

那么 url 将位于每个标记对象中。因此,单击标记后,您可以调用 this.url 并获取 url。

关于javascript - 如何向谷歌地图标记添加唯一链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46849618/

相关文章:

javascript - 在 angularjs 函数中初始化谷歌地图 api

javascript - Three.js Webgl 纹理错误 - 2 的幂 - 没有视频

javascript - 如何在 JSONP 调用中使用回调函数名称包装响应?

c# - 像实时新闻提要ajax刷新一样实现facebook?

Javascript 音频 API : onaudioprocess not fired

c# - 按 ID 或按类型和语言更新资源的 RESTful 约定

ios 谷歌地图的准确性

javascript - 将 Bootstrap 3 工具提示添加到 google maps API v3 标记

javascript - 正则表达式:优先匹配?

javascript - 在 Reactjs 中异步加载谷歌分析嵌入 api 脚本