forked from xkjyeah/vue-google-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyline-editing.html
115 lines (104 loc) · 3.2 KB
/
polyline-editing.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="root">
<label>
Start at:
<gmap-autocomplete @place_changed="updateCenter($event)" />
</label>
<gmap-map :center="center" :zoom="12" style="width: 100%; height: 500px" ref="map">
<gmap-polyline v-if="path.length > 0" :path="path" :editable="true" @path_changed="updateEdited($event)"
@rightclick="handleClickForDelete"
ref="polyline">
</gmap-polyline>
</gmap-map>
<div>
<textarea :value="polylineGeojson" style="width: 100%; height: 200px"
@input="readGeojson">
</textarea>
<div v-if="errorMessage">{{errorMessage}}</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.0/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
<script src="vue-google-maps.js"></script>
<script>
Vue.use(VueGoogleMaps, {
load: {
key: 'AIzaSyDf43lPdwlF98RCBsJOFNKOkoEjkwxb5Sc',
libraries: 'places',
},
});
function closeLoop (path) {
return path.concat(path.slice(0, 1))
}
document.addEventListener('DOMContentLoaded', function() {
window.XXX = new Vue({
el: '#root',
data: {
center: {lat: 1.38, lng: 103.8},
edited: null,
path: [
{lat: 1.33, lng: 103.75},
{lat: 1.43, lng: 103.85},
],
mvcPath: null,
errorMessage: null,
polylineGeojson: '',
},
watch: {
polylinePath: _.throttle(function (path) {
if (path) {
this.path = path
this.polylineGeojson = JSON.stringify({
type: 'Polyline',
coordinates: this.path.map(({lat, lng}) => [lng, lat])
}, null, 2)
}
}, 1000)
},
computed: {
polylinePath: function () {
if (!this.mvcPath) return null
let path = [];
for (let j=0; j<this.mvcPath.getLength(); j++) {
let point = this.mvcPath.getAt(j);
path.push({lat: point.lat(), lng: point.lng()});
}
return path
},
},
methods: {
updateCenter: function (place) {
this.center = {
lat: place.geometry.location.lat(),
lng: place.geometry.location.lng(),
}
},
updateEdited: function (mvcPath) {
this.mvcPath = mvcPath
},
handleClickForDelete($event) {
if ($event.vertex) {
this.$refs.polyline.$polylineObject.getPaths()
.getAt($event.path)
.removeAt($event.vertex)
}
},
readGeojson: function ($event) {
try {
this.polylineGeojson = $event.target.value
var v = JSON.parse($event.target.value);
this.path = v.coordinates
.map(([lng, lat]) => ({lat, lng}))
this.errorMessage = null
} catch (err) {
this.errorMessage = err.message
}
}
}
});
});
</script>
</body>