-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #795 from geonetwork/mv-geocoding-providers
[Map-Viewer] Added Geocoding service for Multiple Providers
- Loading branch information
Showing
6 changed files
with
162 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Injectable, Inject, InjectionToken } from '@angular/core' | ||
import { | ||
queryGeoadmin, | ||
GeoadminOptions, | ||
GeocodingResult, | ||
queryGeonames, | ||
GeonamesOptions, | ||
DataGouvFrOptions, | ||
queryDataGouvFr, | ||
} from '@geospatial-sdk/geocoding' | ||
import { from, Observable, throwError } from 'rxjs' | ||
import { catchError } from 'rxjs/operators' | ||
|
||
type GeoadminGeocodingProvider = ['geoadmin', GeoadminOptions] | ||
type GeonamesGeocodingProvider = ['geonames', GeonamesOptions] | ||
type DataGouvFrGeocodingProvider = ['data-gouv-fr', DataGouvFrOptions] | ||
export type GeocodingProvider = | ||
| GeoadminGeocodingProvider | ||
| GeonamesGeocodingProvider | ||
| DataGouvFrGeocodingProvider | ||
|
||
export const GEOCODING_PROVIDER = new InjectionToken<GeocodingProvider>( | ||
'geocoding-provider' | ||
) | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class GeocodingService { | ||
constructor( | ||
@Inject(GEOCODING_PROVIDER) private provider: GeocodingProvider | ||
) {} | ||
|
||
query(text: string): Observable<GeocodingResult[]> { | ||
let queryObservable: Observable<GeocodingResult[]> | ||
switch (this.provider[0]) { | ||
case 'geoadmin': | ||
queryObservable = from( | ||
queryGeoadmin(text, this.provider[1] as GeoadminOptions) | ||
) | ||
break | ||
case 'geonames': | ||
queryObservable = from( | ||
queryGeonames(text, this.provider[1] as GeonamesOptions) | ||
) | ||
break | ||
case 'data-gouv-fr': | ||
queryObservable = from( | ||
queryDataGouvFr(text, this.provider[1] as DataGouvFrOptions) | ||
) | ||
break | ||
default: | ||
return throwError( | ||
() => new Error(`Unsupported geocoding provider: ${this.provider[0]}`) | ||
) | ||
} | ||
return queryObservable.pipe(catchError((error) => throwError(error))) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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