编辑 1
这是git上的源代码
我正在开发一个带有 Mapbox 集成的 Ionic-Vue 项目。我的问题是我无法更新我的 geojson 数据源以在地图上创建标记,geoJson 数据作为道具传入。这是流程
在 APP.vue 上创建了钩子
- 使用电容器获取用户当前位置。
- .then() 对服务器进行 Axios 调用以获取 JSON 数据
- 成功后通过突变设置要存储的数据
在 Home.vue [TheMap.vue 的父组件]
- 调用的计算值
getGeoJson
调用 getter 从保存较早步骤的状态中获取 geojson 数据 - 将道具与要作为道具发送的 TheMap 组件上的
:marker
计算值数据绑定。getGeoJson
- 监听事件
$updatedLocation
并调用 API 操作来获取新的 geojson 数据。
在 TheMap.vue 上
- 在创建 Hook 时调用
createMap()
. - 在方法中:
createMap()
- 再次使用获取用户坐标
capacitorJS
- 初始化 mapbox 地图并将其保存到
this.map
变量 - 添加归因使用
this.map.addControl
this.map.on("load",cb)
在 cb call.addSource()
& thenaddMarkers()
- 为当前用户新建一个
marker
并保存到cosntUserLocationMarker
UserLocationMarker.on("dragend",CB)
发出具有最新当前用户位置的事件
- 再次使用获取用户坐标
这是相同的代码,只是放置脚本标签而不是整个.vue 文件
APP.Vue
export default {
name: "App",
data() {
return {
currentLocation: "",
};
},
created() {
Geolocation.getCurrentPosition()
.then((result) => {
this.currentLocation = [
result.coords.longitude,
result.coords.latitude,
];
this.$store.dispatch(
`lists/${POST_GEOJSON_ACTION}`,
this.currentLocation
);
})
.catch((err) => console.log(err));
},
};
Home.vue
<template>
<the-map :markers="geojson" @updatedLocation="updateMap"></the-map>
</template>
<script>
export default {
name: "Home",
computed: {
getGeoJson() {
return this.$store.getters[`lists/${GET_GEOJSON_GETTER}`];
},
},
methods: {
updateMap(center) {
this.$store.dispatch(`lists/${POST_GEOJSON_ACTION}`, center);
},
},
};
</script>
TheMap.vue
export default {
props: ["markers"],
emits: ["updatedLocation"],
data() {
return {
access_token: process.env.VUE_APP_MAP_ACCESS_TOKEN,
center: [0, 0],
map: {},
};
},
mounted() {
this.createMap();
},
methods: {
async createMap() {
try {
const coords = await Geolocation.getCurrentPosition();
this.center = [coords.coords.longitude, coords.coords.latitude];
mapboxgl.accessToken = this.access_token;
//Map Instance
this.map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/userName/API_KEY",
center: this.center,
zoom: 12,
scrollZoom: true,
});
// Custom Attribution over the map for Branding
this.map.addControl(
new mapboxgl.AttributionControl({
customAttribution: ` © Comapny Name`,
})
);
this.map.on("load", function(e) {
this.map.addSource("places", {
type: "geojson",
data: this.markers,
});
this.addMarkers();
});
const UserLocationMarker = new mapboxgl.Marker({
draggable: true,
})
.setLngLat(this.center)
.addTo(this.map);
UserLocationMarker.on("dragend", async (e) => {
this.center = Object.values(e.target.getLngLat());
this.$emit("updatedLocation", this.center);
});
} catch (err) {
console.log(err);
}
},
addMarkers() {
this.markers.features.forEach(function(marker) {
var el = document.createElement("div");
el.id = "marker-" + marker.properties.id;
el.className = "marker";
new mapboxgl.Marker(el, { offset: [0, -23] })
.setLngLat(marker.geometry.coordinates)
.addTo(this.map);
});
},
},
};
我的问题是TheMap.vue
确实得到undefined
| [{geojson}]
但是,作为道具,它不会在初始化时甚至在父组件中更改源之后加载标记。
我期望的是,加载上的地图使用标记道具来构建标记列表(如果可用) else show nothing[handle undefined |
null`] 如果在更改的位置上注入了一组新数据作为道具,则更新该标记列表。