【发布时间】: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