简体   繁体   English

Google Maps的标记群集链接

[英]Marker Cluster Links with Google Maps

So I'm making a real estate website, and the client has asked that all properties can be viewed on a map. 因此,我正在制作一个房地产网站,客户要求所有属性都可以在地图上查看。 I thought the best way to do this to avoid it looking too cluttered would be to use Marker Clusters. 我认为最好的方法是使用标记群集,以免显得过于混乱。 However, I need each individual marker to link to that specific properties page. 但是,我需要每个单独的标记链接到该特定的属性页面。 I'm not too experienced with Javascript so I'm struggling to crack this one. 我对Javascript不太了解,因此我很难破解。

Right now, the map is completely unresponsive (You cant move or zoom the map) and no markers are displayed. 目前,该地图完全没有响应(您无法移动或缩放该地图),并且不会显示任何标记。

This is my code so far, where am I going wrong? 到目前为止,这是我的代码,我在哪里出错?

<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>

Firstly, you call initMap once Google's JS loads, but you try creating the markers immediately. 首先,Google的JS加载后,您会调用initMap,但是尝试立即创建标记。 Add that code into initMap, or at least into another function that's called from initMap. 将该代码添加到initMap中,或至少添加到从initMap中调用的另一个函数中。

Secondly, you create the marker without specifying its map, which you need to do. 其次,您无需指定标记就可以创建标记,您需要这样做。 So add map: map to your marker's properties. 因此,添加map: map到标记的属性。

Thirdly, you create map as a local variable to your initMap function, so it won't be accessible where you currently create your marker (unless you move that into the initMap function), or where you create your MarkerClusterer. 第三,您将map创建为initMap函数的局部变量,因此无法在当前创建标记的位置(除非将其移入initMap函数)或创建MarkerClusterer的位置访问地图。 Either put all the code that references map in the same function, or make map a global variable. 可以将所有引用map的代码放在同一函数中,或者将map为全局变量。

Also you seem to have a JS error where you create the map, I don't see the closing }) you need. 另外,您在创建地图时似乎遇到了JS错误,我看不到所需的结束})

And there was an error in how you're using the Array.map() callback. 而且在使用Array.map()回调时发生错误。

Here's an amended version: 这是一个修订的版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM