-
Notifications
You must be signed in to change notification settings - Fork 10
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
1 parent
4ac6d78
commit a36e362
Showing
3 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
import "https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.min.js"; | ||
// TODO: point to npm when released | ||
import "https://cdn.jsdelivr.net/gh/jimmyrocks/maplibre-gl-vector-text-protocol@main/dist/maplibre-gl-vector-text-protocol.min.js"; | ||
import "https://cdn.jsdelivr.net/npm/@maplibre/[email protected]/dist/maplibre-gl-geocoder.min.js"; | ||
|
||
export function init(ctx, data) { | ||
ctx.importCSS( | ||
"https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.min.css" | ||
); | ||
|
||
ctx.importCSS( | ||
"https://cdn.jsdelivr.net/npm/@maplibre/[email protected]/lib/maplibre-gl-geocoder.min.css" | ||
); | ||
|
||
ctx.root.innerHTML = ` | ||
<div id='map' style='width: 896px; height: 400px;'></div> | ||
`; | ||
|
@@ -19,6 +24,7 @@ export function init(ctx, data) { | |
controls = [], | ||
locate = [], | ||
terrain = [], | ||
geocode = [], | ||
hover = [], | ||
center = [], | ||
info = [], | ||
|
@@ -46,6 +52,9 @@ export function init(ctx, data) { | |
terrain.forEach(() => { | ||
addTerrain(); | ||
}); | ||
geocode.forEach(() => { | ||
addGeocode(); | ||
}); | ||
hover.forEach((layer) => { | ||
addHover(layer); | ||
}); | ||
|
@@ -88,6 +97,10 @@ export function init(ctx, data) { | |
addTerrain(); | ||
}); | ||
|
||
ctx.handleEvent("add_geocode", () => { | ||
addGeocode(); | ||
}); | ||
|
||
ctx.handleEvent("clusters_expansion", (clusters) => { | ||
inspectClusters(clusters); | ||
}); | ||
|
@@ -143,6 +156,10 @@ export function init(ctx, data) { | |
map.addControl(terrain); | ||
} | ||
|
||
function addGeocode() { | ||
map.addControl(geocoder()); | ||
} | ||
|
||
function loadImage({ name, url, options }) { | ||
map.loadImage(url, (error, image) => { | ||
if (error) throw error; | ||
|
@@ -220,4 +237,44 @@ export function init(ctx, data) { | |
map.getCanvas().style.cursor = ""; | ||
}); | ||
} | ||
|
||
function geocoder() { | ||
const geocoderApi = { | ||
forwardGeocode: async (config) => { | ||
const features = []; | ||
try { | ||
const request = `https://nominatim.openstreetmap.org/search?q=${config.query}&format=geojson&polygon_geojson=1&addressdetails=1`; | ||
const response = await fetch(request); | ||
const geojson = await response.json(); | ||
for (const feature of geojson.features) { | ||
const center = [ | ||
feature.bbox[0] + (feature.bbox[2] - feature.bbox[0]) / 2, | ||
feature.bbox[1] + (feature.bbox[3] - feature.bbox[1]) / 2, | ||
]; | ||
const point = { | ||
type: "Feature", | ||
geometry: { | ||
type: "Point", | ||
coordinates: center, | ||
}, | ||
place_name: feature.properties.display_name, | ||
properties: feature.properties, | ||
text: feature.properties.display_name, | ||
place_type: ["place"], | ||
center, | ||
}; | ||
features.push(point); | ||
} | ||
} catch (e) { | ||
console.error(`Failed to forwardGeocode with error: ${e}`); | ||
} | ||
|
||
return { | ||
features, | ||
}; | ||
}, | ||
}; | ||
|
||
return new MaplibreGeocoder(geocoderApi, { maplibregl }); | ||
} | ||
} |
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