【问题标题】:Marker Cluster Links with Google Maps与谷歌地图的标记集群链接
【发布时间】:2017-05-25 10:17:49
【问题描述】:

所以我正在制作一个房地产网站,客户要求可以在地图上查看所有房产。我认为避免它看起来过于混乱的最佳方法是使用标记集群。但是,我需要每个单独的标记链接到该特定属性页面。我对 Javascript 不太熟悉,所以我正在努力破解这个。

现在,地图完全没有响应(您无法移动或缩放地图)并且没有显示任何标记。

这是我目前的代码,我哪里出错了?

<script>
function initMap() {
   var map = new google.maps.Map(document.getElementById('map'), {
   zoom: 7,
   center: {<?php echo convertAddressToLngLat('Hastings, East Sussex');?>}
});

var markers = locations.map(function(link, location, i) {
    var marker =  new google.maps.Marker({
        position: location,
        url: link //Will be different for each marker
    });

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

 // 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 = [
     ["www.google.com", {lat: 50.8542590, lng: 0.5734530}],
 ]
 </script>

<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>

<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDlrUspRmip7Verfu7IDtW-FYkkum1QZMs&callback=initMap"></script>

【问题讨论】:

  • 贴出的代码有什么问题?请提供minimal reproducible example在问题本身(您如何加载 API,如何加载 MarkerClusterer)
  • 对不起,应该把它放在问题中。地图没有响应,并且我当前的代码没有显示任何标记。
  • 您错误地使用了locations.map
  • 抱歉,我是新手,当然也不是 Javascript 或 Maps API 方面的专家。您能否详细说明并解释我是如何错误使用它的?

标签: javascript google-maps google-maps-api-3 markerclusterer


【解决方案1】:

首先,您在 Google 的 JS 加载后调用 initMap,但您尝试立即创建标记。将该代码添加到 initMap 中,或至少添加到从 initMap 调用的另一个函数中。

其次,您创建标记而不指定其地图,这是您需要做的。因此,将 map: map 添加到标记的属性中。

第三,您将 map 创建为 initMap 函数的局部变量,因此在您当前创建标记的位置(除非您将其移动到 initMap 函数中)或创建 MarkerClusterer 的位置将无法访问它。 要么将所有引用map 的代码放在同一个函数中,要么将map 设为全局变量。

另外,您在创建地图时似乎有一个 JS 错误,我没有看到您需要的结束 })

您在使用Array.map() 回调时出现错误。

这是一个修改后的版本:

function initMap() {
   var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 7,
       center: {<?php echo convertAddressToLngLat('Hastings, East Sussex');?>}
   });

   var locations = [
     ["www.google.com", {lat: 50.8542590, lng: 0.5734530}],
    ];

    var markers = locations.map(function(location) {
        var marker = new google.maps.Marker({
            map: map,
            position: location[1],
            url: location[0] //Will be different for each marker
        });

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

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

【讨论】:

  • 我试图让它运行,但无济于事。我用我试图运行的完全相同的代码创建了一个 JSFiddle,你能看出它有什么问题吗? jsfiddle.net/tnze1c92
  • 啊,您使用.map() 回调的方式有误,我已经更新了答案
  • 完美!非常感谢,你让我很开心!
猜你喜欢
  • 2013-03-24
  • 2016-04-03
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多