forked from mcguffin/acf-openstreetmap-field
-
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
Showing
3 changed files
with
111 additions
and
75 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
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 } |
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