javascript - 标记集群不起作用
问题描述
我用标记制作了谷歌地图,但我有很多标记所以想添加标记cluterer,但它似乎不起作用。在控制台中显示“未定义标记”。
var urlApi = "https://api.jcdecaux.com/vls/v1/stations?contract=lyon&apiKey=";
function initMap(){
//map options
var options ={
zoom:13,
center:{lat:45.7563172, lng:4.827523}
}
//new map
var map = new google.maps.Map(document.getElementById('map'),options);
//new markers
$.getJSON(urlApi, function(data) {
data.forEach(function(item) {
var marker = new google.maps.Marker({
position : {
lat : parseFloat(item.position.lat),
lng : parseFloat (item.position.lng)
},
map:map
})
})
});
var markerCluster = new MarkerClusterer(map, marker,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
<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=&callback=initMap"></script>
<script src="animation-fleche.js"></script>
<script src="slider.js"></script>
<script src="script.js"></script>
<script src="map.js"></script>
</body>
解决方案
您创建标记,但不会将它们保存在任何地方。您需要创建一个 Marker 数组,然后将其传递给 MarkerCluster 构造函数:
$.getJSON(urlApi, function(data) {
const markerArray = data.map(function(item) {
return new google.maps.Marker({
position : {
lat : parseFloat(item.position.lat),
lng : parseFloat (item.position.lng)
});
});
var markerCluster = new MarkerClusterer(map, markerArray,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
});
推荐阅读
- fiware - Fiware 查询中忽略大小写
- reactjs - 从回调函数访问使用 useState 定义的状态变量
- context-free-grammar - 来自语言问题的上下文无关语法
- jquery - jquery 函数停止在 github 页面上工作
- python - 运行从 GitHub 下载的大脑提取的 Python 项目
- php - 完全不相关的异常,同时保存多对多关系学说和php
- stock - 表盘的 Fitbit 库存数据
- oracle - 存储过程 - 无法显示未找到数据消息
- wix - WIX 使用引导程序安装所需的软件。错误“提取失败”
- c# - ASP.NET Core 3.1 URL 参数返回不正确的参数值