首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在vue中更改arcgis地图位置onClick

在vue中更改arcgis地图位置onClick
EN

Stack Overflow用户
提问于 2020-04-19 13:35:06
回答 2查看 97关注 0票数 1

我正在尝试使用vue onClick将地图更新到我的当前位置,它会更新道具并将它们发送到我的地图组件。当我的地图数据发生变化时,我使用a :key重新呈现我的地图组件,并且我的地图中心获得了一些新的x,y。(基于esri/arcgis示例,我需要重新构建地图,如果有人知道这是错误的,请告诉我)

VUE js arcgis启动文档:https://developers.arcgis.com/javascript/latest/guide/vue/

由于某些原因,我的地图再次渲染,看起来它即将加载,但随后它仍然是空白的。也许有人可以告诉我,这是不是组件在我强制它再次渲染后仍然以某种方式持续存在的问题?

我的app.vue

代码语言:javascript
复制
<template>
    <div id="app">
        <web-map v-bind:centerX="lat" v-bind:centerY="long" ref="mapRef"/>

        <div class="center">
          <b-button class="btn-block" @click="getLocation" 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: -118,
        long: 34,
      }
    },
    methods:{

      showPos(pos){
        this.lat = pos.coords.latitude
        this.long = pos.coords.longitude

        this.$refs.mapRef.updateCoordinates()
        console.log('new location',this.lat,this.long, this.$refs)   
      },


      getLocation(){
        if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(this.showPos);
          } else { 
            console.log("Geolocation is not supported by this browser.");
          }
      },

    },
};
</script>

我的地图组件

代码语言:javascript
复制
<template>
  <div></div>
</template>

<script>
import { loadModules } from 'esri-loader';

export default {
  name: 'web-map',
  props:['centerX', 'centerY'],
  data: function(){
    return{
      X: this.centerX,
      Y: this.centerY,
      view: null
    }
  },
  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: [this.X,this.Y],   ///USE PROPS HERE FOR NEW CENTER
        zoom: 8
      });
    });
  },
  beforeDestroy() {
    if (this.view) {
      // destroy the map view
      this.view.container = null;
    }
  },
  methods:{
    updateCoordinates(){
        this.view.centerAt([this.X,this.Y])
      }
  }

};

</script>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-19 14:52:46

我不认为您作为道具传递给web-map的键有任何用途,因为它没有在组件中使用。

您可以尝试强制更新组件,如下所示:

代码语言:javascript
复制
<web-map v-bind:centerX="lat" v-bind:centerY="long" ref="mapRef" />
代码语言:javascript
复制
this.refs.mapRef.$forceUpdate()

这可以确保强制更新整个组件,但也许有更好的解决方案。而不是重新呈现整个组件,这意味着必须再次创建地图,您可以让组件保持活动状态,只需使用事件来更新坐标。

基于https://developers.arcgis.com/javascript/3/jsapi/map-amd.html#centerat,您可以使用centerAt方法将地图重新居中。

这样,map组件就有了如下方法:

代码语言:javascript
复制
updateCoordinates(coord){
  this.view.centerAt(coord)
}

您可以使用以下命令在父级上调用它

代码语言:javascript
复制
this.refs.mapRef.updateCoordinates(newCenter)

希望这对你有所帮助,如果你有任何进展,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2020-04-19 13:46:28

我认为您可以使用setInterVal()测试Watch,以便每隔1秒检查一次您的位置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61300002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档