Skip to content

Commit 5372a21

Browse files
redzjovimandaputtra
authored andcommitted
content: Membuat coordinate field di Nuxt.js dengan Vuetifyjs, Leafletjs, dan OpenStreetMap.
1 parent cf07606 commit 5372a21

File tree

3 files changed

+245
-1
lines changed

3 files changed

+245
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
## Development
1616

1717
1. Install deps `yarn`
18-
2. Run project `yarn develop`, open at `http://localhost:8080`
18+
2. Run project `yarn dev`, open at `http://localhost:8080`
1919
3. Build project `yarn build`
2020

2121
## Menerbitkan Tulisanmu pada Blog Vue.js ID
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
---
2+
title: Membuat coordinate field di Nuxt.js dengan Vuetifyjs, Leafletjs, dan OpenStreetMap
3+
date: 2020-10-17
4+
author: redzjovi
5+
published: true
6+
tags: ['nuxt', 'vuetify', 'leaflet', 'openstreetmap']
7+
series: false
8+
cover_image: ./images/shared/vuejs-id-logo.jpeg
9+
canonical_url: false
10+
description: Membuat coordinate field di Nuxt.js dengan Vuetifyjs, Leafletjs, dan OpenStreetMap
11+
---
12+
13+
Kita akan membuat komponen koordinat dengan menggunakan [text-field](https://vuetifyjs.com/en/components/text-fields/) pada [Vuetify](https://vuetifyjs.com/), peta [Leaflet](https://leafletjs.com/) pada [nuxt-leaflet](https://github.com/schlunsen/nuxt-leaflet), dan juga pencarian dengan provider [OpenStreetMap](https://smeijer.github.io/leaflet-geosearch/providers/openstreetmap) dengan [leaflet-geosearch](https://github.com/smeijer/leaflet-geosearch).
14+
15+
Langkah-langkah:
16+
17+
1. Menginstall Nuxt
18+
19+
2. Menginstall Vuetify
20+
21+
3. Membuat komponen koordinat
22+
23+
4. Menginstall Leaflet
24+
25+
5. Memakai dialog dan peta OpenStreetMap
26+
27+
## 1. Menginstall Nuxt
28+
29+
Sesuai dengan panduan installasi [Nuxt.js](https://nuxtjs.org/guide/installation/), pada kali ini saya menggunakan [create-nuxt-app](https://github.com/nuxt/create-nuxt-app).
30+
31+
```bash
32+
npx create-nuxt-app <project-name>
33+
```
34+
35+
Kemudian pindah ke direktori tersebut dan jalankan pengembangan.
36+
37+
```bash
38+
cd <project-name>
39+
```
40+
41+
```bash
42+
npm run dev
43+
```
44+
45+
Jika sudah berhasil akan terlihat logo Nuxt.js.
46+
47+
## 2. Menginstall Vuetify
48+
49+
Pada kali ini saya menggunakan [vuetify-module](https://github.com/nuxt-community/vuetify-module) dari [nuxt-community](https://github.com/nuxt-community).
50+
51+
```bash
52+
npm install--save @nuxtjs/vuetify
53+
```
54+
55+
Tambahkan juga `@nuxtjs/vuetify` di `modules` pada `nuxt.config.js`
56+
57+
```js
58+
export default {
59+
modules: [
60+
'@nuxtjs/vuetify'
61+
]
62+
}
63+
```
64+
65+
## 3. Membuat komponent koordinat
66+
67+
Buat file `components/vuetify/CoordinateField.vue` dengan `text-field` di dalamnya.
68+
69+
```js
70+
<template>
71+
<div>
72+
<v-text-field
73+
@change="textChange($event)"
74+
v-bind="$attrs"
75+
v-model="text.value"
76+
autocomplete="off"
77+
prepend-inner-icon="mdi-map-marker"
78+
>
79+
</v-text-field>
80+
</div>
81+
</template>
82+
83+
<script>
84+
export default {
85+
props: {
86+
value: {
87+
default: '0,0',
88+
type: String
89+
}
90+
},
91+
data() {
92+
return {
93+
text: {
94+
value: this.value
95+
}
96+
}
97+
},
98+
watch: {
99+
value: function(value) {
100+
this.text.value = value
101+
}
102+
},
103+
methods: {
104+
textChange(value) {
105+
this.$emit('input', value)
106+
}
107+
}
108+
}
109+
</script>
110+
```
111+
112+
Dan ubah `pages/index.vue` untuk memanggil komponen tersebut.
113+
114+
```js
115+
<template>
116+
<v-container>
117+
<CoordinateField
118+
v-model="coordinate"
119+
clearable
120+
dense
121+
label="Coordinate"
122+
outlined
123+
/>
124+
</v-container>
125+
</template>
126+
127+
<script>
128+
import CoordinateField from '~/components/vuetify/CoordinateField'
129+
130+
export default {
131+
components: {
132+
CoordinateField
133+
},
134+
data() {
135+
return {
136+
coordinate: '-6.229728,106.6894312'
137+
}
138+
}
139+
}
140+
</script>
141+
```
142+
143+
Sehingga tampilan menjadi
144+
145+
![coordinate-field](./images/membuat-coordinate-field-di-nuxtjs-dengan-vuetifyjs-leafletjs-dan-openstreetmap/1.png)
146+
147+
## 4. Menginstall Leaflet
148+
149+
Beberapa paket yang dipakai:
150+
151+
- [nuxt-leaflet](https://github.com/schlunsen/nuxt-leaflet)
152+
153+
```bash
154+
npm install --save nuxt-leaflet
155+
```
156+
157+
Tambahkan `nuxt-leaflet` pada `modules` di `nuxt.config.js`.
158+
159+
```js
160+
{
161+
modules: [
162+
'nuxt-leaflet'
163+
]
164+
}
165+
```
166+
167+
- [vue2-leaflet-fullscreen](https://github.com/mikeu/vue2-leaflet-fullscreen) untuk melakukan full screen.
168+
169+
```bash
170+
npm install --save vue2-leaflet-fullscreen
171+
```
172+
173+
Saya juga membuat `/plugins/leafleat.js` untuk meregistrasi komponen `LControlFullscreen`
174+
175+
```js
176+
import Vue from 'vue'
177+
import LControlFullscreen from 'vue2-leaflet-fullscreen'
178+
179+
Vue.component('l-control-fullscreen', LControlFullscreen)
180+
```
181+
182+
Tambahkan `leaflet.js` dengan mode `client` pada `plugins` di `nuxt.config.js`
183+
184+
```js
185+
{
186+
plugins: [
187+
{
188+
mode: 'client',
189+
src: '~/plugins/leaflet'
190+
}
191+
}
192+
```
193+
194+
- [leaflet-geosearch](https://github.com/smeijer/leaflet-geosearch) untuk melakukan pencarian
195+
196+
```bash
197+
npm install --save leaflet-geosearch
198+
```
199+
200+
Saya juga membuat `/plugins/leafletGeosearch.js` dan melakukan inject `leafletGeosearch` untuk meinisialisasi class `OpenStreetMapProvider` pada objek `provider`
201+
202+
```js
203+
import { OpenStreetMapProvider } from 'leaflet-geosearch'
204+
205+
export default (context, inject) => {
206+
const leafletGeosearch = {}
207+
leafletGeosearch.provider = new OpenStreetMapProvider()
208+
209+
inject('leafletGeosearch', leafletGeosearch)
210+
}
211+
```
212+
213+
Tambahkan `leafletGeosearch.js` dengan mode `client` pada `plugins` di `nuxt.config.js`
214+
215+
```js
216+
{
217+
plugins: [
218+
{
219+
mode: 'client',
220+
src: '~/plugins/leafletGeosearch'
221+
}
222+
}
223+
```
224+
225+
## 5. Memakai dialog dan peta OpenStreetMap
226+
227+
Langkah-langkah:
228+
229+
- Memakai komponen `v-dialog`
230+
- Menambahkan komponen `l-map` untuk menampilkan peta
231+
- Menambahkan komponent `v-autocomplete` pada `l-control` di `l-map`
232+
- Menambahkan metode `mapSearchItemsFetch()` untuk menggunakan `$leafletGeosearch.provider.search()`
233+
- Menambahkan komponen `l-control-fullscreen` untuk melakukan full screen
234+
- Menambahkan metode `mapDrag()` untuk memperbaharui `latitude`, `longitude` jika diseret
235+
- Menambahkan `v-btn` dengan icon `gps`, dan menambahkan metode `mapGpsClick()` untuk mendapatkan posisi saat ini
236+
237+
<iframe src="https://codesandbox.io/embed/redzjovi-nuxt-vuetify-leaflet-coordinate-field-92m59?fontsize=14&hidenavigation=1&theme=dark"
238+
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
239+
title="redzjovi-nuxt-vuetify-leaflet-coordinate-field"
240+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
241+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
242+
></iframe>
243+
244+
[Demo](https://codesandbox.io/s/redzjovi-nuxt-vuetify-leaflet-coordinate-field-92m59?file=/pages/index.vue)

0 commit comments

Comments
 (0)