Skip to content

Commit

Permalink
WIP switch geocoders mcguffin#71
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille37 committed Dec 24, 2024
1 parent 8476e5e commit 1197381
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 37 deletions.
102 changes: 102 additions & 0 deletions src/js/lib/media/views/geocoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {L} from 'osm-map';

const { i18n } = acf_osm_admin

class Geocoder {

static resolveAndConfigure(which, options )
{
console.debug('Geocoder.resolveAndConfigure()', 'which', which);
switch( which )
{
case 'nominatim':
return Geocoder.useNominatim( options );
case 'photon':
return Geocoder.usePhoton( options );
}
return null ;
}

static useNominatim( options )
{
const nominatim_options = Object.assign( {
// geocodingQueryParams: {'accept-language':'it'},
// reverseQueryParams: {'accept-language':'it'},
htmlTemplate: result => {
var parts = [],
templateConfig = {
interpolate: /\{(.+?)\}/g
},
addr = _.defaults( result.address, {
building:'',
road:'',
house_number:'',

postcode:'',
city:'',
town:'',
village:'',
hamlet:'',

state:'',
country:'',
} );

parts.push( _.template( i18n.address_format.street, templateConfig )( addr ) );
parts.push( _.template( i18n.address_format.city, templateConfig )( addr ) );
parts.push( _.template( i18n.address_format.country, templateConfig )( addr ) );

return parts
.map( el => el.replace(/\s+/g,' ').trim() )
.filter( el => el !== '' )
.join(', ')
}
}, options.nominatim);

const gc = L.Control.Geocoder.nominatim( nominatim_options )
return gc ;

}

static usePhoton( options )
{
const photon_options = Object.assign( {
htmlTemplate: result => {
var parts = [],
templateConfig = {
interpolate: /\{(.+?)\}/g
},
addr = _.defaults( result.address, {
building:'',
road:'',
house_number:'',

postcode:'',
city:'',
town:'',
village:'',
hamlet:'',

state:'',
country:'',
} );

parts.push( _.template( i18n.address_format.street, templateConfig )( addr ) );
parts.push( _.template( i18n.address_format.city, templateConfig )( addr ) );
parts.push( _.template( i18n.address_format.country, templateConfig )( addr ) );

return parts
.map( el => el.replace(/\s+/g,' ').trim() )
.filter( el => el !== '' )
.join(', ')
}

}, options.photon );

const gc = L.Control.Geocoder.photon( photon_options )
return gc ;
}

}

export {Geocoder}
47 changes: 10 additions & 37 deletions src/js/lib/media/views/map-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { MarkerData, MapData } from 'media/models';
import {MarkerEntry} from 'media/views/marker-entry';
import { uniqid } from 'misc/uniquid';

import {Geocoder} from 'media/views/geocoder' ;

const { options, i18n } = acf_osm_admin
const instances = []

Expand Down Expand Up @@ -422,42 +424,12 @@ class MapInput extends Backbone.View {
// add an extra control panel region for out search
this.map._controlCorners['above'] = $above.get(0);

const nominatim_options = Object.assign( {
// geocodingQueryParams: {'accept-language':'it'},
// reverseQueryParams: {'accept-language':'it'},
htmlTemplate: result => {
var parts = [],
templateConfig = {
interpolate: /\{(.+?)\}/g
},
addr = _.defaults( result.address, {
building:'',
road:'',
house_number:'',

postcode:'',
city:'',
town:'',
village:'',
hamlet:'',

state:'',
country:'',
} );

parts.push( _.template( i18n.address_format.street, templateConfig )( addr ) );

parts.push( _.template( i18n.address_format.city, templateConfig )( addr ) );

parts.push( _.template( i18n.address_format.country, templateConfig )( addr ) );

return parts
.map( el => el.replace(/\s+/g,' ').trim() )
.filter( el => el !== '' )
.join(', ')
}
}, options.nominatim),
geocoder_options = Object.assign({

//const leafletGeocoder = Geocoder.resolveAndConfigure( 'nominatim', options );
const leafletGeocoder = Geocoder.resolveAndConfigure( 'photon', options );
console.debug('leafletGeocoder', leafletGeocoder);

const geocoder_options = Object.assign({
collapsed: false,
position: 'above',
placeholder: i18n.search,
Expand All @@ -468,7 +440,8 @@ class MapInput extends Backbone.View {
queryMinLength:3,
defaultMarkGeocode:false,
// geocodingQueryParams: {'accept-language':'de_DE'},
geocoder: L.Control.Geocoder.nominatim( nominatim_options )
//geocoder: L.Control.Geocoder.nominatim( nominatim_options )
geocoder: leafletGeocoder
}, options.geocoder );

this.geocoder = L.Control.geocoder( geocoder_options )
Expand Down

0 comments on commit 1197381

Please sign in to comment.