gpt4 book ai didi

vue.js - MapBox super 集群错误的集群位置

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:35 25 4
gpt4 key购买 nike

我正在用 mapbox supercluster 制作聚类 map .我面临的问题是集群不在正确的位置。例如,我在荷兰只有狗,但当缩小时它们也在法国。

enter image description here

当我进一步放大时,星团会慢慢移动到荷兰。当我滚动时,集群也会在 map 上移动。

问题可以在我的网站上看到https://v2.sayhi.dog/?dog-map=true .

我的代码如下所示(我使用 vue.js ):

<template>
<div class="w-full h-full" id="map"></div>
</template>

<script>
import mapboxgl from 'mapbox-gl'
import Supercluster from 'supercluster';

export default {
props: ['locations'],

data() {
return {
map: null,
copyLocations: this.locations,
clusters: [],
markers: [],
clustersGeojson: {},
clusterIndex: null,
}
},

mounted() {
mapboxgl.accessToken = 'my-token';
// init the map
this.map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/sayhi/cjkp9e59x3m3y2rm1zbcc0c',
zoom: 2,
});

this.addControls();
this.map.on("load", () => { this.addMarkers() });
this.map.addControl(new mapboxgl.NavigationControl());
},

methods: {
addMarkers() {
this.clusterIndex = new Supercluster({
radius: 40,
maxZoom: 10
});

this.clusterIndex.load(this.locations.features);
this.map.on('moveend', () => { this.moveEnd(); });
this.updateClusters();
},

updateClusters() {
var bounds = this.map.getBounds(),
zoom = this.map.getZoom();

this.clustersGeojson = this.clusterIndex.getClusters([
bounds.getWest(),
bounds.getSouth(),
bounds.getEast(),
bounds.getNorth()
], Math.floor(zoom));

if (Object.keys(this.clusters).length) {
this.clusters.forEach(function(cluster) {
cluster.remove();
});
}

this.displayFeatures(this.clustersGeojson);
},

addControls() {
this.map.addControl(new mapboxgl.NavigationControl(), "bottom-right");
this.map.addControl(new mapboxgl.FullscreenControl(), "bottom-right");
},

moveEnd() {
this.updateClusters();
},

displayFeatures(features) {
if (this.markers.length) {
this.markers.forEach(function(marker) {
marker.remove();
});
}

features.forEach((feature) => {
var isCluster = (!!feature.properties.cluster) ? true : false,
$feature;

if (isCluster) {
var leaf = this.clusterIndex.getLeaves(feature.properties.cluster_id)[0];
$feature = document.createElement("div");
$feature.className = 'flex items-center justify-center w-12 h-12 rounded-full text-center text-white font-bold shadow bg-cover cursor-pointer bg-center';
$feature.style.backgroundImage = `url(${leaf.properties.image})`;

var $inner = document.createElement("div");
$inner.innerHTML = feature.properties.point_count_abbreviated;

$feature.appendChild($inner);
this.clusters[feature.properties.cluster_id] = new mapboxgl.Marker($feature).setLngLat(feature.geometry.coordinates).addTo(this.map);
} else {
$feature = document.createElement('div');
$feature.className = 'flex items-center justify-center w-12 h-12 rounded-full text-center text-white font-bold shadow bg-cover cursor-pointer bg-center';
$feature.style.backgroundImage = `url(${feature.properties.image})`;

this.markers.push(new mapboxgl.Marker($feature).setLngLat(feature.geometry.coordinates).addTo(this.map));
}
});
}
}
}
</script>

https://gist.github.com/larsjanssen6/ebb5d7e3887c885af4c65d294ca1fb77

这是一个非常令人沮丧的问题,我现在已经苦苦挣扎了好几天,非常感谢您的帮助。

--更新--

我做了一个 JSFiddle:

https://jsfiddle.net/obhstrvz/10/

希望有人能帮忙。

最佳答案

加载页面时,开发人员工具控制台中会显示以下警告:

This page appears to be missing CSS declarations for Mapbox GL JS, which may cause the map to display incorrectly. Please ensure your page includes mapbox-gl.css, as described in https://www.mapbox.com/mapbox-gl-js/api/.

虽然 Mapbox GL JS API 引用不包含任何与 mapbox-gl.css 相关的说明,您可以在示例中找到它的用法。将以下行添加到 <head>您的页面部分通过开发人员工具消除了各种位置问题(除了标记之外,还表现为缺少 map 控件):

<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.0/mapbox-gl.css' rel='stylesheet' />

请注意,您可能需要使用其他版本来匹配您的 mapbox-gl.js 的版本文件。

关于vue.js - MapBox super 集群错误的集群位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com