forked from odoe/vue-jsapi4
/
map.vue
139 lines (129 loc) · 3.58 KB
/
map.vue
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<template>
<section class="container">
<img src="../assets/img/logo.png" alt="Nuxt.js Logo" class="logo" />
<h1 class="title">
This page is loaded from the {{ name }}
</h1>
<h2 class="info" v-if="name === 'client'">
Please refresh the page
</h2>
<div id="viewDiv"></div>
<nuxt-link class="button" to="/">
Home page
</nuxt-link>
<nuxt-link class="button" to="/about">
About page
</nuxt-link>
</section>
</template>
<script>
// import * as esriLoader from 'esri-loader'
// now we only need the loadModules() function
import { loadModules } from 'esri-loader'
export default {
data ({ req }) {
return {
name: req ? 'server' : 'client'
}
},
head () {
return {
title: `Map Page (${this.name}-side)`
}
},
mounted () {
console.log('map: mounted')
const createMap = () => {
// first, we use Dojo's loader to require the map class
// esriLoader.dojoRequire([
// now we use loadModules() instead of dojoRequire()
loadModules([
'esri/Map',
'esri/views/SceneView',
'esri/core/watchUtils'
],
// now we include the options we would have passed to bootstrap()
// as the second argument to loadModules
{
// use a specific version instead of latest 4.x
url: 'https://js.arcgis.com/4.2/'
}
// now loadModules returns a promise so the callback gets passed to .then()
).then(([EsriMap, SceneView, watchUtils]) => {
// create map with the given options at a DOM node w/ id 'mapNode'
let map
if (!this.$store.state.map) {
map = new EsriMap({
basemap: 'satellite',
ground: 'world-elevation'
})
this.$store.state.map = map
} else {
map = this.$store.state.map
}
const view = new SceneView({
container: 'viewDiv',
map,
camera: this.$store.state.camera
})
this.$store.state.watchHandle = watchUtils.watch(view, 'camera', (camera) => {
console.log('change')
this.$store.state.camera = camera.clone().toJSON()
})
// NOTE: important: now that we're using a promise
// your callback must NOT return any v4.x classes that resolve to promises
// this will cause a hole in the space-time continum that will kill us all
// return view
})
}
// now with loadModules() we no longer need any of this business
// has the ArcGIS API been added to the page?
// if (!esriLoader.isLoaded()) {
// // no, lazy load it the ArcGIS API before using its classes
// esriLoader.bootstrap((err) => {
// if (err) {
// console.error(err)
// }
// // once it's loaded, create the map
// createMap()
// }, {
// // use a specific version instead of latest 4.x
// url: 'https://js.arcgis.com/4.2/'
// })
// } else {
// // ArcGIS API is already loaded, just create the map
// createMap()
// }
// instead we just call createMap()
// in fact, createMap() is no longer needed
// we could have just called loadModules()
createMap()
},
beforeDestroy () {
console.log('map: beforeDestroy')
this.$store.state.watchHandle.remove()
}
}
</script>
<style scoped>
@import url('https://js.arcgis.com/4.2/esri/css/main.css');
#viewDiv {
height: 500px;
width: 100%;
}
.title
{
margin-top: 50px;
}
.info
{
font-weight: 300;
color: #9aabb1;
margin: 0;
margin-top: 10px;
}
.button
{
margin-top: 50px;
}
</style>