view.goTo 地图呈现空白 arcgis
view.goTo map renders blank arcgis
我正在使用 arcgis 地图,我正在尝试通过在我的地图视图上调用 goTo() 来更新地图中心,但由于某种原因,地图只是变为空白并且从不更新,我正在记录新的坐标,它们是正确的。
我在这里使用参考文档:https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html
有 arcgis 经验的人可以帮助我吗?我知道这不是我的代码的具体问题,但它可能是 vue 和组件渲染的问题,因为它与 arcgis
相关
到目前为止我已经尝试过了
- 摆脱道具并在本地更新组件内的所有内容
- 使用按键强制重新渲染组件
有趣的是,如果我只是为我的新位置输入一些幻数,地图就会正确更新,但是当我使用一些函数来获取位置然后将其传入时,它不起作用,只会显示作为空白地图
我的app.vue
<template>
<div id="app">
<web-map v-bind:centerX="lat" v-bind:centerY="long" ref="map"/>
<div class="center">
<b-button class="btn-block" @click="updateCenter()" variant="primary">My Location</b-button>
</div>
</div>
</template>
<script>
import WebMap from './components/webmap.vue';
export default {
name: 'App',
components: { WebMap },
data(){
return{
lat: null,
long: null,
}
},
methods:{
updateCenter(){
this.$refs.map.getLocation()
}
},
};
</script>
我的地图组件
<template>
<div></div>
</template>
<script>
import { loadModules } from 'esri-loader';
export default {
name: 'web-map',
data: function(){
return{
X: -118,
Y: 34,
}
},
mounted() {
console.log('new data',this.X,this.Y)
// lazy load the required ArcGIS API for JavaScript modules and CSS
loadModules(['esri/Map', 'esri/views/MapView'], { css: true })
.then(([ArcGISMap, MapView]) => {
const map = new ArcGISMap({
basemap: 'topo-vector'
});
this.view = new MapView({
container: this.$el,
map: map,
center: [-118,34], ///USE PROPS HERE FOR NEW CENTER
zoom: 8
});
});
},
beforeDestroy() {
if (this.view) {
// destroy the map view
this.view.container = null;
}
},
methods:{
showPos(pos){
console.log('new location',pos.coords.latitude,pos.coords.longitude)
this.view.goTo({center:[pos.coords.latitude,pos.coords.longitude]})
},
getLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPos);
} else {
console.log("Geolocation is not supported by this browser.");
}
},
}
};
</script>
这里有一些有用的东西:
https://codesandbox.io/s/frosty-glitter-39wpe?file=/src/App.vue
我是从头开始做的,只是从你那里取了一小部分,并将它们与 ArcGIS 的一些例子结合起来(我一点也不熟悉)。
需要注意的一件事是 .goTo({center: [lat, long]})
没有按预期工作:它一直在某个海洋的中央。
然后我从 esri
导入 Point 并将 center
作为 new Point(long, lat)
传递,这似乎产生了预期的结果。因为它有效,我没有进一步看,但我想它应该在没有转换的情况下可行。您可能需要传递坐标系或类似的东西。
据我所知,您的示例中的问题在于您尝试将数据从父级传递给子级的方式。你期望 this.$refs.map
是一个 Vue 实例,但它不是。这是一个 DOM 元素。它基本上是 Vue 实例的 $el
。从父实例访问子方法并不是那么简单。
另一件需要注意的事情是,即使您在示例中将 centerX
和 centerY
绑定到 child 上,您似乎也从未使用过它们(但我想这只是您尝试使用 props 时遗留下来的! ?)。
无论如何,在我的示例中,我选择简单地更新子项的 coords 属性,同时使用 watch
fn 来处理重新居中。
切换:
this.view = new MapView({
container: this.$el,
map: map,
center: [-118,34], ///USE PROPS HERE FOR NEW CENTER
zoom: 8
});
至
this.view = new MapView({
container: this.$el,
map: map });
this.view.center.longitude = -118;
this.view.center.latitude = 34;
this.view.zoom = 8;
Tao的另一个答案在.goTo({center: []}) 方法调用中向后long/latitude,这就是它去海洋的原因:https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#goTo
我正在使用 arcgis 地图,我正在尝试通过在我的地图视图上调用 goTo() 来更新地图中心,但由于某种原因,地图只是变为空白并且从不更新,我正在记录新的坐标,它们是正确的。
我在这里使用参考文档:https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html
有 arcgis 经验的人可以帮助我吗?我知道这不是我的代码的具体问题,但它可能是 vue 和组件渲染的问题,因为它与 arcgis
相关到目前为止我已经尝试过了 - 摆脱道具并在本地更新组件内的所有内容 - 使用按键强制重新渲染组件
有趣的是,如果我只是为我的新位置输入一些幻数,地图就会正确更新,但是当我使用一些函数来获取位置然后将其传入时,它不起作用,只会显示作为空白地图
我的app.vue
<template>
<div id="app">
<web-map v-bind:centerX="lat" v-bind:centerY="long" ref="map"/>
<div class="center">
<b-button class="btn-block" @click="updateCenter()" variant="primary">My Location</b-button>
</div>
</div>
</template>
<script>
import WebMap from './components/webmap.vue';
export default {
name: 'App',
components: { WebMap },
data(){
return{
lat: null,
long: null,
}
},
methods:{
updateCenter(){
this.$refs.map.getLocation()
}
},
};
</script>
我的地图组件
<template>
<div></div>
</template>
<script>
import { loadModules } from 'esri-loader';
export default {
name: 'web-map',
data: function(){
return{
X: -118,
Y: 34,
}
},
mounted() {
console.log('new data',this.X,this.Y)
// lazy load the required ArcGIS API for JavaScript modules and CSS
loadModules(['esri/Map', 'esri/views/MapView'], { css: true })
.then(([ArcGISMap, MapView]) => {
const map = new ArcGISMap({
basemap: 'topo-vector'
});
this.view = new MapView({
container: this.$el,
map: map,
center: [-118,34], ///USE PROPS HERE FOR NEW CENTER
zoom: 8
});
});
},
beforeDestroy() {
if (this.view) {
// destroy the map view
this.view.container = null;
}
},
methods:{
showPos(pos){
console.log('new location',pos.coords.latitude,pos.coords.longitude)
this.view.goTo({center:[pos.coords.latitude,pos.coords.longitude]})
},
getLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(this.showPos);
} else {
console.log("Geolocation is not supported by this browser.");
}
},
}
};
</script>
这里有一些有用的东西:
https://codesandbox.io/s/frosty-glitter-39wpe?file=/src/App.vue
我是从头开始做的,只是从你那里取了一小部分,并将它们与 ArcGIS 的一些例子结合起来(我一点也不熟悉)。
需要注意的一件事是 .goTo({center: [lat, long]})
没有按预期工作:它一直在某个海洋的中央。
然后我从 esri
导入 Point 并将 center
作为 new Point(long, lat)
传递,这似乎产生了预期的结果。因为它有效,我没有进一步看,但我想它应该在没有转换的情况下可行。您可能需要传递坐标系或类似的东西。
据我所知,您的示例中的问题在于您尝试将数据从父级传递给子级的方式。你期望 this.$refs.map
是一个 Vue 实例,但它不是。这是一个 DOM 元素。它基本上是 Vue 实例的 $el
。从父实例访问子方法并不是那么简单。
另一件需要注意的事情是,即使您在示例中将 centerX
和 centerY
绑定到 child 上,您似乎也从未使用过它们(但我想这只是您尝试使用 props 时遗留下来的! ?)。
无论如何,在我的示例中,我选择简单地更新子项的 coords 属性,同时使用 watch
fn 来处理重新居中。
切换:
this.view = new MapView({
container: this.$el,
map: map,
center: [-118,34], ///USE PROPS HERE FOR NEW CENTER
zoom: 8
});
至
this.view = new MapView({
container: this.$el,
map: map });
this.view.center.longitude = -118;
this.view.center.latitude = 34;
this.view.zoom = 8;
Tao的另一个答案在.goTo({center: []}) 方法调用中向后long/latitude,这就是它去海洋的原因:https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#goTo