Tulisan ini dibuat pada tanggal 26 Februari 2019.
1.1 Inisialisasi
Install node (https://nodejs.org/).
Buat folder baru (contoh : new-project)
kemudian jalankan
npm init
dan jawab beberapa pertanyaan yang diajukan.
Maka hasilnya adalah sebuah file package.json dengan isi
sbb:
{
"name": "new-project",
"version": "1.0.0",
"description": "tes
development openlayers",
"main": "index.js",
"scripts": {
"test": "echo \"Error:
no test specified\" && exit 1"
},
"author": "sujana",
"license": "ISC"
}
1.2 install module openlayers
npm install ol
maka
hasilnya akan terbentuk file package-lock.json
dan folder-folder dependency
yang terdaftar pada file tersebut

secara default module openlayers yang diinstall adalah versi
terakhir dirilis (saat ini versi 5.3.1).
Kemudian aplikasi kita akan dikemas (bundle) menggunakan Parcel (https://parceljs.org/). Oleh karena itu kita install terlebih dahulu.
npm install --save-dev
parcel-bundler
1.3 Membuat peta pertama
buat
index.js
import'ol/ol.css';
import {Map, View} from'ol';
import TileLayer from'ol/layer/Tile';
import OSM from'ol/source/OSM';
constmap =newMap({
target:'map',
layers: [
newTileLayer({
source:newOSM()
})
],
view:newView({
center: [0,0],
zoom:0
})
});
Catatan:
1.
pada index.js perintah pemanggilan unit dapat
juga dilakukan dengan cara sebagai berikut:
import 'ol/ol.css';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';
2.
center : [0, 0]
Bukan menunjukkan koordinat geografis (Long, Lat), tapi menunjukkan
koordinat bidang datar (x,y) dengan proyeksi mengikuti layer OSM yaitu EPSG 3857.
Sehingga untuk kota Bogor menjadi:
center: [11885983,
-735116]
1.4 Tambahkan
Script untuk memanggil Aplikasi
tambahkan pada file
package.json
{
"name": "new-project",
"version": "1.0.0",
"description": "tes development openlayers",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "sujana",
"license": "ISC",
"dependencies": {
"ol": "^5.3.1"
},
"devDependencies": {
"parcel-bundler": "^1.11.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}
Untuk memanggil aplikasi, jalankan perintah:
npm start
Kemudian lihat pada browser http://localhost:1234
Sekian tutorial Openlayers dalam membuat peta pertama..
Comments
Post a Comment