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 25, 2024
1 parent 1197381 commit 182eefc
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 75 deletions.
11 changes: 11 additions & 0 deletions include/ACFFieldOpenstreetmap/Core/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

class Core extends Plugin {

const GEOCODER_OPENCAGE = 'opencage';
const GEOCODER_NOMINATIM = 'nominatim';
const GEOCODER_DEFAUlT = self::GEOCODER_NOMINATIM ;
const GEOCODERS = [self::GEOCODER_NOMINATIM, self::GEOCODER_OPENCAGE];

private $leaflet_providers = null;

/**
Expand Down Expand Up @@ -129,6 +134,7 @@ public function register_assets() {
* )
*/
'geocoder' => apply_filters( 'acf_osm_geocoder_options', [] ),
'geocoder_name' => defined('ACF_OSM_GEOCODER_NAME') ? constant('ACF_OSM_GEOCODER_NAME') : self::GEOCODER_DEFAUlT,
/**
* Filter Nominatim geocoder options.
*
Expand All @@ -145,6 +151,11 @@ public function register_assets() {
'geocoderQueryParams' => [ 'accept-language' => $language, ],
'reverseQueryParams' => [ 'accept-language' => $language, ],
]),
'opencage' => apply_filters( 'acf_osm_opencage_options', [
'apiKey' => null,
'geocodingQueryParams' => null,
'reverseQueryParams' => null,
]),
],
'i18n' => [
'search' => __( 'Search...', 'acf-openstreetmap-field' ),
Expand Down
129 changes: 74 additions & 55 deletions src/js/lib/media/views/geocoder.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,121 @@
import {L} from 'osm-map';
/**
*
*/

const { i18n } = acf_osm_admin
import { L } from 'osm-map';

const { options, i18n } = acf_osm_admin

class Geocoder {

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

static useNominatim( options )
{
const nominatim_options = Object.assign( {
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:'',
addr = _.defaults(result.address, {
building: '',
road: '',
house_number: '',

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

state:'',
country:'',
} );
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 ) );
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 !== '' )
.map(el => el.replace(/\s+/g, ' ').trim())
.filter(el => el !== '')
.join(', ')
}
}, options.nominatim);

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

}

static useOpenrouteservice(options) {
const ors_options = Object.assign({

}, options.openrouteservice);
const gc = L.Control.Geocoder.openrouteservice(ors_options)
return gc;
}

static useOpenCage(options) {
const oc_options = Object.assign({
}, options.opencage);
const gc = new L.Control.Geocoder.OpenCage(oc_options);
return gc;
}

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

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

state:'',
country:'',
} );
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 ) );
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 !== '' )
.map(el => el.replace(/\s+/g, ' ').trim())
.filter(el => el !== '')
.join(', ')
}

}, options.photon );
}, options.photon);

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

}

export {Geocoder}
export { Geocoder }
46 changes: 26 additions & 20 deletions src/js/lib/media/views/map-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,28 +424,27 @@ class MapInput extends Backbone.View {
// add an extra control panel region for out search
this.map._controlCorners['above'] = $above.get(0);


//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,
errorMessage: i18n.nothing_found,
showResultIcons:true,
suggestMinLength:3,
suggestTimeout:250,
queryMinLength:3,
defaultMarkGeocode:false,
// geocodingQueryParams: {'accept-language':'de_DE'},
//geocoder: L.Control.Geocoder.nominatim( nominatim_options )
geocoder: leafletGeocoder
}, options.geocoder );
const leafletGeocoder = Geocoder.build( options.geocoder_name );

const geocoder_options = Object.assign({
collapsed: false,
position: 'above',
placeholder: i18n.search,
errorMessage: i18n.nothing_found,
showResultIcons:true,
suggestMinLength:3,
suggestTimeout:250,
queryMinLength:3,
defaultMarkGeocode:false,
// geocodingQueryParams: {'accept-language':'de_DE'},
//geocoder: L.Control.Geocoder.nominatim( nominatim_options )
geocoder: leafletGeocoder
}, options.geocoder );

this.geocoder = L.Control.geocoder( geocoder_options )
.on( 'markgeocode', e => {

// search result click
let model,
previousGeocode = false;
Expand All @@ -471,7 +470,6 @@ console.debug('leafletGeocoder', leafletGeocoder);

}


if ( this.canAddMarker ) {
// infinite markers or markers still in range
model = this.markers.add( marker_data );
Expand Down Expand Up @@ -568,13 +566,21 @@ console.debug('leafletGeocoder', leafletGeocoder);
}

parseGeocodeResult( results, latlng ) {

console.debug('parseGeocodeResult', results);

var label = false;

if ( ! results.length ) {
label = latlng.lat + ', ' + latlng.lng;
} else {
$.each( results, ( i, result ) => {
label = result.html;
if( result.html )
label = result.html ;
else if( result.name )
label = result.name ;
else
label = 'Failed to decode geocoder result 😕';
});
}
// trim
Expand Down

0 comments on commit 182eefc

Please sign in to comment.