如何在Vue中保存Google地图编辑的多边形路径

雨果·范·沙克维克

我有一个Vue组件BaseMap,其中包含一个标记组件Location。“位置”组件具有可以被编辑的“多边形”标记。

// BaseMap
<GmapMap>
  <Location :location="location" />
</GmapMap>


// Location
<gmap-marker>
  <gmap-polygon
    ref="polyMarker"
    :editable="true"
    :path="location.path"
    @mouseup="updatePath('polyMarker')"
  />
</gmap-marker>

<script>

  methods: {
   updatePath(ref_name){
     console.log(this.$refs[ref_name].path) // same array as origin
   }
  }

</script>

如何访问多边形的新编辑点?如果我使用this。$ ref.polyMarker.path,我将继续获取原始的点数组而不是新的点。

编辑

在与MrUpsidown讨论之后,我继续并在codesandbox中编写了最小的应用程序。存在相同的问题。

Dependancies:
- vue
- vue2-gmap-cuspom-marker

//////////////////////////////////////////////////////////////
// main.js
import Vue from "vue";
import App from "./App.vue";
import * as VueGoogleMaps from "vue2-google-maps";

Vue.config.productionTip = false;

Vue.use(VueGoogleMaps, {
  load: {
    //    libraries: "places", // This is required if you use the Autocomplete plugin
    key: "[USE YOUR OWN KEY]"
  }
  //  autobindAllEvents: false,
  //  installComponents: true
});
new Vue({
  render: h => h(App)
}).$mount("#app");


//////////////////////////////////////////
//App.vue

<template>
  <div id="app">
    <HelloWorld />
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";

export default {
  name: "App",
  components: {
    HelloWorld
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>


//////////////////////////////////////////////////////
// HelloWorld.vue

<template>
  <div>
    <baseMap/>
  </div>
</template>

<script>
import baseMap from "@/components/baseMap";

export default {
  name: "HelloWorld",
  components: { baseMap },
  props: {
    msg: String
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
</style>


////////////////////////////////////////////////////
//baseMap.vue

<template>
  <div>
    <GmapMap :style="'height:500px'" :center="center" :zoom="zoom">
      <Location :location="location"/>
    </GmapMap>
  </div>
</template>

<script>
import Location from "@/components/Location";

export default {
  name: "baseMap",
  components: { Location },
  data: function() {
    return {
      center: { lat: -33.9249, lng: 18.4241 },
      zoom: 12,
      location: {
        path: [
          { lat: -33.91170210440241, lng: 18.422548745513268 },
          { lat: -33.90993912517883, lng: 18.422806237578698 },
          { lat: -33.90874597713464, lng: 18.42422244393856 },
          { lat: -33.90482806012767, lng: 18.42952248895199 },
          { lat: -33.90073186345211, lng: 18.42428681695492 },
          { lat: -33.90128397100101, lng: 18.420596097350426 },
          { lat: -33.90256627151344, lng: 18.417656396270104 },
          { lat: -33.90367045927834, lng: 18.416454766631432 },
          { lat: -33.90532671411109, lng: 18.417913888335534 },
          { lat: -33.908389810302396, lng: 18.413579438567467 },
          { lat: -33.91084733115123, lng: 18.41703412377865 },
          { lat: -33.91170210440241, lng: 18.422548745513268 }
        ]
      }
    };
  },
  props: {
    msg: String
  }
};
</script>

<style>
</style>



/////////////////////////////////////////////////////////
// Location.vue

<template>
  <div>
    <gmap-marker>
      <gmap-polygon
        ref="polyMarker"
        :editable="true"
        :path="location.path"
        @mouseup="updatePath('polyMarker')"
      />
    </gmap-marker>
  </div>
</template>

<script>
import GmapCustomMarker from "vue2-gmap-custom-marker";

export default {
  name: "Location",
  components: {
    GmapCustomMarker
  },
  props: {
    location: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  methods: {
    updatePath(name) {
      console.log(this.$refs[name].path);
    }
  }
};
</script>

<style>
</style>
埃文

首先替换:path:paths

:paths="location.path"

然后使用as作为参数替换@mouseup事件@paths_changed$event

@paths_changed="updatePath($event)"

最后记录您的路径;您会看到它现在如何更新。

updatePath(mvcArray) {
  for (let i = 0; i < mvcArray.getLength(); i++) {
    for (let j = 0; j < mvcArray.getAt(i).getLength(); j++) {
      console.log(mvcArray.getAt(i).getAt(j).lat() + "," + mvcArray.getAt(i).getAt(j).lng());
    }
  }
  console.log("-------")
}

为了获得更多指导,我建议您查看库示例,例如this

希望这对您有所帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Google地图上删除绘制的多边形

阻止Google地图多边形进行编辑。

如何在MongoDB中保存的geoJson中查找多边形

如何在MongoDB中保存的geoJson中查找多边形

如何在Google地图的彩色多边形上显示街道号码?

如何在Google地图片段Android Studio中导入KML多边形文件

GoogleMaps:如何在单个Google地图中具有多边形和标记

如何在地图中显示多个多边形

如何在地图上添加多边形

如何从Google地图中删除多边形

Google Maps:如何从SVG路径创建多边形

Google地图-缩放到多边形

Google顶部的地图路线多边形

Google地图:透明多边形

在Google地图上绘制多边形

虚线多边形Google地图

如何在Google地图上以距用户输入点已定义的距离(米)绘制多边形(矩形)

谷歌地图可编辑/可拖动多边形

在R中保存多边形的填充区域

带有外部地图的Google地图多边形

使用“ PolyUtil.encode()”在Google地图上保存重要的多边形是否安全?

使用Google地图在多边形内绘制较小的多边形

将Google地图的相邻多边形组合成单个多边形的方法。

如何在R中的地图中仅过滤一个多边形

如何在谷歌地图中使用latlng的字符串数组绘制多边形

如何在谷歌地图中显示存储为字符串的多边形坐标?

如何在Angular中的地图上动态绘制多边形的形状

如何在与国家R传单对应的地图多边形上分配弹出窗口

如何在谷歌地图 URL 中获取多边形并在 gmaps js 中使用它