Skip to content

Commit

Permalink
Merge pull request #230 from lnu-norge/instant-search-by-location
Browse files Browse the repository at this point in the history
Instant search by location
  • Loading branch information
DanielJackson-Oslo authored Feb 21, 2024
2 parents aa3d368 + cd9dab4 commit 8621882
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions app/javascript/controllers/mapbox_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ export default class extends Controller {
this.map.on('moveend', (obj) => {
if (obj.wasZoom) {
this.reloadPosition();

}
});

Expand Down Expand Up @@ -316,11 +315,11 @@ export default class extends Controller {
const url =`https://ws.geonorge.no/stedsnavn/v1/sted?sok=${event.target.value}&fuzzy=true`
const result = await (await fetch(url)).json();

if(result.navn.length === 0)
if(!result || !result.navn || result.navn.length === 0)
return;

const place = result.navn[0]
this.flyToGeoNorgeLocation(place)
this.moveMapToGeoNorgeLocation(place)
}

removeMarker(key) {
Expand Down Expand Up @@ -375,11 +374,11 @@ export default class extends Controller {
});
}

flyToGeoNorgeLocation(place) {
moveMapToGeoNorgeLocation(place) {
const location = place.geojson.geometry;

if (location.type === "Point") {
this.flyToPoint(location.coordinates)
this.moveMapTo(location.coordinates)
}

if (location.type === "MultiPoint") {
Expand All @@ -393,27 +392,31 @@ export default class extends Controller {
bounds.extend(coord);
}
return this.map.fitBounds(bounds, {
padding: 100
}, { wasZoom: true });
padding: 100,
animate: false,
maxZoom: 13
}, {
wasZoom: true,
});
}

// Unsure about location type given, use the center given by representasjonspunkt instead
const point = [
place.representasjonspunkt.øst,
place.representasjonspunkt.nord
];
return this.flyToPoint(point);
return this.moveMapTo(point);
}

flyToPoint(point) {
this.map.flyTo({ center: point, zoom: 12 }, { wasZoom: true });
moveMapTo(point) {
this.map.jumpTo({ center: point, zoom: 12 }, { wasZoom: true });
}

async enableLocationService() {
const position = await this.requestPosition();

if(position != null) {
this.flyToPoint([position.coords.longitude, position.coords.latitude]);
this.moveMapTo([position.coords.longitude, position.coords.latitude]);
}
else {
alert('Kunne ikke hente lokasjon. Sjekk at lokasjon er skrudd på')
Expand Down

0 comments on commit 8621882

Please sign in to comment.