-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hsieh Chin Fan
committed
Nov 13, 2024
1 parent
6f032bd
commit 1697f8c
Showing
5 changed files
with
140 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import defaultExport, { loadCSS } from './BaseRenderer' | ||
import { | ||
renderWith, | ||
renderByYamlWith, | ||
renderByScriptTargetWith, | ||
} from './mapclay.mjs' | ||
import * as L from 'leaflet/dist/leaflet-src.esm' | ||
import { TerraDrawLeafletAdapter } from 'terra-draw' | ||
loadCSS('https://unpkg.com/[email protected]/dist/leaflet.css') | ||
|
||
/** class: Leaflet */ | ||
const Renderer = class extends defaultExport { | ||
/** fields */ | ||
id = 'leaflet' | ||
version = '1.9.4' | ||
L = L | ||
|
||
/** options: center, zoom */ | ||
addMap ({ target, center, zoom }) { | ||
const [x, y] = center | ||
this.map = L.map(target).setView([y, x], zoom) | ||
|
@@ -26,13 +24,15 @@ const Renderer = class extends defaultExport { | |
return this.map | ||
} | ||
|
||
/** options: draw */ | ||
getTerraDrawAdapter ({ draw, L, map }) { | ||
if (!draw) return { state: 'skip' } | ||
|
||
this.terraDrawAdapter = new TerraDrawLeafletAdapter({ lib: L, map }) | ||
return this.getTerraDrawAdapter | ||
} | ||
|
||
/** options: control */ | ||
setControl ({ map, control }) { | ||
if (!control || Object.values(control).filter(v => v).length === 0) { return { state: 'skip' } } | ||
|
||
|
@@ -71,7 +71,7 @@ const Renderer = class extends defaultExport { | |
return new L.GridLayer.GridDebug() | ||
} | ||
|
||
// Configure extra stuff | ||
/** options: debug, eval */ | ||
setExtra (config) { | ||
const { map, debug } = config | ||
if (!debug && !config.eval) return { state: 'skip' } | ||
|
@@ -87,32 +87,7 @@ const Renderer = class extends defaultExport { | |
} | ||
} | ||
|
||
addMarker (config) { | ||
const options = config.element | ||
? { | ||
html: config.element.innerHTML, | ||
iconSize: config.size, | ||
iconAnchor: config.anchor, | ||
} | ||
: { | ||
html: this.svgPin.html, | ||
iconSize: this.svgPin.size, | ||
iconAnchor: this.svgPin.anchor, | ||
} | ||
const markerIcon = L.divIcon({ | ||
...options, | ||
className: 'marker', | ||
}) | ||
const xy = Array.from(config.xy).reverse() | ||
const marker = L.marker(xy, { icon: markerIcon }) | ||
.addTo(this.map) | ||
const element = marker.getElement() | ||
element.classList.add('marker') | ||
|
||
element.remove = () => this.map.removeLayer(marker) | ||
return element | ||
} | ||
|
||
/** options: data */ | ||
addTileData ({ map, data }) { | ||
const tileData = data.filter(d => d.type === 'tile') | ||
|
||
|
@@ -136,39 +111,34 @@ const Renderer = class extends defaultExport { | |
} | ||
} | ||
|
||
addGPXFile ({ map, data }) { | ||
const gpxUrl = data.find(record => record.type === 'gpx') | ||
if (!gpxUrl) return { state: 'skip' } | ||
|
||
const script = document.createElement('script') | ||
script.src = | ||
'https://cdnjs.cloudflare.com/ajax/libs/leaflet-gpx/1.7.0/gpx.min.js' | ||
document.body.append(script) | ||
/** actions: marker */ | ||
addMarker (config) { | ||
const options = config.element | ||
? { | ||
html: config.element.innerHTML, | ||
iconSize: config.size, | ||
iconAnchor: config.anchor, | ||
} | ||
: { | ||
html: this.svgPin.html, | ||
iconSize: this.svgPin.size, | ||
iconAnchor: this.svgPin.anchor, | ||
} | ||
const markerIcon = L.divIcon({ | ||
...options, | ||
className: 'marker', | ||
}) | ||
const xy = Array.from(config.xy).reverse() | ||
const marker = L.marker(xy, { icon: markerIcon }) | ||
.addTo(this.map) | ||
const element = marker.getElement() | ||
element.classList.add('marker') | ||
|
||
const options = { | ||
gpx_options: { | ||
joinTrackSegments: false, | ||
}, | ||
polyline_options: { | ||
color: 'red', | ||
weight: 3, | ||
lineCap: 'round', | ||
}, | ||
marker_options: { | ||
startIconUrl: null, | ||
endIconUrl: null, | ||
shadowUrl: '', | ||
wptIconUrls: { | ||
'': null, | ||
}, | ||
}, | ||
async: true, | ||
} | ||
script.onload = () => { | ||
new L.GPX(gpxUrl, options).addTo(map) | ||
} | ||
element.remove = () => this.map.removeLayer(marker) | ||
return element | ||
} | ||
|
||
/** actions: camera */ | ||
async updateCamera ({ center, zoom, bounds, animation, padding, duration }) { | ||
const latLon = center ? L.latLng(center[1], center[0]) : this.map.getCenter() | ||
const options = { | ||
|
@@ -196,6 +166,7 @@ const Renderer = class extends defaultExport { | |
}) | ||
} | ||
|
||
/** utils: projection */ | ||
project ([lng, lat]) { | ||
return this.map.latLngToContainerPoint([lat, lng]) | ||
} | ||
|
@@ -206,4 +177,5 @@ const Renderer = class extends defaultExport { | |
} | ||
} | ||
|
||
/** export */ | ||
export default Renderer |
Oops, something went wrong.