-
-
Notifications
You must be signed in to change notification settings - Fork 273
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
6 changed files
with
164 additions
and
19 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
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,53 @@ | ||
--- | ||
name: GeoApiFr | ||
menu: Providers | ||
route: /providers/geoapifr | ||
--- | ||
|
||
import Playground from '../components/Playground'; | ||
import Map from '../components/Map'; | ||
|
||
# GeoApiFr Provider | ||
|
||
For more options and configurations, see the [Geo Api FR][1]. | ||
|
||
<Playground> | ||
<Map provider="GeoApiFr" /> | ||
</Playground> | ||
|
||
```js | ||
import { GeoApiFrProvider } from 'leaflet-geosearch'; | ||
|
||
const provider = new GeoApiFrProvider(); | ||
|
||
// add to leaflet | ||
import { GeoSearchControl } from 'leaflet-geosearch'; | ||
|
||
map.addControl( | ||
new GeoSearchControl({ | ||
provider, | ||
}), | ||
); | ||
``` | ||
|
||
## Optional parameters | ||
|
||
Geo Api FR supports a number of [optional parameters][2]. As the api requires those parameters to be added to the url, they can be added to the `params` key of the provider. | ||
|
||
All options defined next to the `params` key, would have been added to the request body. | ||
|
||
```js | ||
const provider = new OpenStreetMapProvider({ | ||
searchUrl: 'https://api-adresse.data.gouv.fr/search', | ||
reverseUrl: 'https://api-adresse.data.gouv.fr/reverse', | ||
params: { | ||
type: 'municipality', // limit search results to city | ||
autocomplete: 1, // Use in autocomplete mode (search in prefix mode) | ||
lat: 0, // Latitude in degree | ||
lon: 0, // Longitude in degree | ||
}, | ||
}); | ||
``` | ||
|
||
[1]: https://geo.api.gouv.fr/ | ||
[2]: https://geo.api.gouv.fr/adresse |
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,84 @@ | ||
import AbstractProvider, { | ||
EndpointArgument, | ||
ParseArgument, | ||
ProviderOptions, | ||
SearchResult, | ||
RequestType, | ||
} from './provider'; | ||
|
||
export interface RequestResult { | ||
features: RawResult[]; | ||
type: string; | ||
version: string; | ||
attribution: string; | ||
licence: string; | ||
query: string; | ||
limit: string; | ||
} | ||
|
||
export interface RawResult { | ||
properties: { | ||
label: string; | ||
score: number; | ||
importance: number; | ||
x: number; | ||
y: number; | ||
housenumber: string; | ||
id: string; | ||
type: string; | ||
name: string; | ||
postcode: string; | ||
citycode: string; | ||
city: string; | ||
context: string; | ||
street: string; | ||
}; | ||
type: string; | ||
geometry: { | ||
coordinates: number[]; | ||
type: string; | ||
}; | ||
} | ||
|
||
export type GeoApiFrProviderOptions = { | ||
searchUrl?: string; | ||
reverseUrl?: string; | ||
} & ProviderOptions; | ||
|
||
export default class GeoApiFrProvider extends AbstractProvider< | ||
RequestResult, | ||
RawResult | ||
> { | ||
searchUrl: string; | ||
reverseUrl: string; | ||
|
||
constructor(options: GeoApiFrProviderOptions = {}) { | ||
super(options); | ||
|
||
const host = 'https://api-adresse.data.gouv.fr'; | ||
this.searchUrl = options.searchUrl || `${host}/search`; | ||
this.reverseUrl = options.reverseUrl || `${host}/reverse`; | ||
} | ||
|
||
endpoint({ query, type }: EndpointArgument) { | ||
const params = typeof query === 'string' ? { q: query } : query; | ||
|
||
switch (type) { | ||
case RequestType.REVERSE: | ||
return this.getUrl(this.reverseUrl, params); | ||
|
||
default: | ||
return this.getUrl(this.searchUrl, params); | ||
} | ||
} | ||
|
||
parse(result: ParseArgument<RequestResult>): SearchResult<RawResult>[] { | ||
return result.data.features.map((r) => ({ | ||
x: r.geometry.coordinates[0], | ||
y: r.geometry.coordinates[1], | ||
label: r.properties.label, | ||
bounds: null, | ||
raw: r, | ||
})); | ||
} | ||
} |
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