-
-
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.
Added provider for communication with the Geoapify API. --------- Co-authored-by: Stephan Meijer <[email protected]>
- Loading branch information
1 parent
d18b72f
commit 8a7b7f4
Showing
10 changed files
with
253 additions
and
1 deletion.
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
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,40 @@ | ||
--- | ||
name: Geoapify | ||
menu: Providers | ||
route: /providers/geoapify | ||
--- | ||
|
||
import Playground from '../components/Playground'; | ||
import Map from '../components/Map'; | ||
|
||
# Geoapify Provider | ||
|
||
**note**: Geoapify services require an API key. [Obtain geoapify][1]. | ||
For more options and configurations, see the [Geoapify developer docs][2]. | ||
|
||
<Playground> | ||
<Map provider="Geoapify" /> | ||
</Playground> | ||
|
||
```js | ||
import { GeoapifyProvider } from 'leaflet-geosearch'; | ||
|
||
const provider = new GeoapifyProvider({ | ||
params: { | ||
apiKey: '__YOUR_HERE_KEY__', | ||
}, | ||
}); | ||
|
||
// add to leaflet | ||
import { GeoSearchControl } from 'leaflet-geosearch'; | ||
|
||
map.addControl( | ||
new GeoSearchControl({ | ||
provider, | ||
style: 'bar', | ||
}), | ||
); | ||
``` | ||
|
||
[1]: https://apidocs.geoapify.com/ | ||
[2]: https://apidocs.geoapify.com/docs/geocoding/forward-geocoding/#geocode-addresses |
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ export default { | |
'OpenStreetMap', | ||
'Pelias', | ||
'Custom Providers', | ||
'Geoapify', | ||
], | ||
}, | ||
], | ||
|
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,39 @@ | ||
import Provider from '../geoapifyProvider'; | ||
import fixtures from './geoapifyResponse.json'; | ||
|
||
describe('Geoapify', () => { | ||
beforeAll(() => { | ||
fetch.mockResponse(async () => ({ body: JSON.stringify(fixtures) })); | ||
}); | ||
|
||
test('Can fetch results', async () => { | ||
const provider = new Provider({ | ||
params: { | ||
apiKey: process.env.GEOAPIFY_API_KEY, | ||
}, | ||
}); | ||
|
||
const results = await provider.search({ query: 'Chicago' }); | ||
const result = results[0]; | ||
|
||
expect(result.label).toBeTruthy(); | ||
expect(result.x).toEqual(+fixtures.results[0].lon); | ||
expect(result.y).toEqual(+fixtures.results[0].lat); | ||
expect(result.bounds).toBeValidBounds(); | ||
}); | ||
|
||
test.skip('Can get localized results', async () => { | ||
const provider = new Provider({ | ||
params: { | ||
apiKey: process.env.GEOAPIFY_API_KEY, | ||
'accept-language': 'nl', | ||
}, | ||
}); | ||
|
||
const results = await provider.search({ query: 'Chicago' }); | ||
t.is( | ||
results[0].label, | ||
'1214-1224 West Van Buren Street, Chicago, IL 60607, United States of America', | ||
); | ||
}); | ||
}); |
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,65 @@ | ||
{ | ||
"results": [ | ||
{ | ||
"datasource": { | ||
"sourcename": "openstreetmap", | ||
"attribution": "© OpenStreetMap contributors", | ||
"license": "Open Database License", | ||
"url": "https://www.openstreetmap.org/copyright" | ||
}, | ||
"country": "United States", | ||
"country_code": "us", | ||
"state": "Illinois", | ||
"county": "Cook County", | ||
"city": "Chicago", | ||
"postcode": "60607", | ||
"suburb": "Near West Side", | ||
"street": "West Van Buren Street", | ||
"housenumber": "1214-1224", | ||
"lon": -87.65816734222005, | ||
"lat": 41.87695205, | ||
"state_code": "IL", | ||
"formatted": "1214-1224 West Van Buren Street, Chicago, IL 60607, United States of America", | ||
"address_line1": "1214-1224 West Van Buren Street", | ||
"address_line2": "Chicago, IL 60607, United States of America", | ||
"category": "building", | ||
"timezone": { | ||
"name": "America/Chicago", | ||
"offset_STD": "-06:00", | ||
"offset_STD_seconds": -21600, | ||
"offset_DST": "-05:00", | ||
"offset_DST_seconds": -18000, | ||
"abbreviation_STD": "CST", | ||
"abbreviation_DST": "CDT" | ||
}, | ||
"result_type": "building", | ||
"rank": { | ||
"importance": 0.60001, | ||
"popularity": 6.686411370288237, | ||
"confidence": 1, | ||
"confidence_city_level": 1, | ||
"confidence_street_level": 1, | ||
"match_type": "full_match" | ||
}, | ||
"place_id": "515888ea691fea55c0598074fbf63ff04440f00102f9013076880c00000000c00203", | ||
"bbox": { | ||
"lon1": -87.658723, | ||
"lat1": 41.8765924, | ||
"lon2": -87.6576007, | ||
"lat2": 41.8770888 | ||
} | ||
} | ||
], | ||
"query": { | ||
"text": "1214-1224 West Van Buren Street, Chicago, IL 60607, United States of America", | ||
"parsed": { | ||
"housenumber": "1214-1224", | ||
"street": "west van buren street", | ||
"postcode": "60607", | ||
"city": "chicago", | ||
"state": "il", | ||
"country": "united states of america", | ||
"expected_type": "building" | ||
} | ||
} | ||
} |
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,95 @@ | ||
import AbstractProvider, { | ||
EndpointArgument, | ||
ParseArgument, | ||
ProviderOptions, | ||
RequestType, | ||
SearchResult, | ||
} from './provider'; | ||
|
||
export type RequestResult = { | ||
results: RawResult[]; | ||
query: RawQuery[]; | ||
}; | ||
|
||
export interface RawResult { | ||
country: string; | ||
country_code: string; | ||
state: string; | ||
county: string; | ||
city: string; | ||
postcode: number; | ||
suburb: string; | ||
street: string; | ||
lon: string; | ||
lat: string; | ||
state_code: string; | ||
formatted: string; | ||
bbox: BBox; | ||
} | ||
|
||
export interface RawQuery { | ||
text: string; | ||
parsed: RawQueryParsed; | ||
} | ||
|
||
export type RawQueryParsed = { | ||
city: string; | ||
expected_type: string; | ||
}; | ||
|
||
export type BBox = { | ||
lon1: string; | ||
lat1: string; | ||
lon2: string; | ||
lat2: string; | ||
}; | ||
|
||
export type GeoapifyProviderOptions = { | ||
searchUrl?: string; | ||
reverseUrl?: string; | ||
} & ProviderOptions; | ||
|
||
export default class GeoapifyProvider extends AbstractProvider< | ||
RequestResult, | ||
RawResult | ||
> { | ||
searchUrl: string; | ||
reverseUrl: string; | ||
|
||
constructor(options: GeoapifyProviderOptions = {}) { | ||
super(options); | ||
|
||
const host = 'https://api.geoapify.com/v1/geocode'; | ||
this.searchUrl = options.searchUrl || `${host}/search`; | ||
this.reverseUrl = options.reverseUrl || `${host}/reverse`; | ||
} | ||
|
||
endpoint({ query, type }: EndpointArgument): string { | ||
const params = typeof query === 'string' ? { text: query } : query; | ||
params.format = 'json'; | ||
|
||
switch (type) { | ||
case RequestType.REVERSE: | ||
return this.getUrl(this.reverseUrl, params); | ||
|
||
default: | ||
return this.getUrl(this.searchUrl, params); | ||
} | ||
} | ||
|
||
parse(response: ParseArgument<RequestResult>): SearchResult<RawResult>[] { | ||
const records = Array.isArray(response.data.results) | ||
? response.data.results | ||
: [response.data.results]; | ||
return records.map((r) => ({ | ||
x: Number(r.lon), | ||
y: Number(r.lat), | ||
label: r.formatted, | ||
bounds: [ | ||
[parseFloat(r.bbox.lat1), parseFloat(r.bbox.lon1)], // s, w | ||
[parseFloat(r.bbox.lat2), parseFloat(r.bbox.lon2)], // n, e | ||
], | ||
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