-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AMap(Gaode) provider (#409)
Co-authored-by: Stephan Meijer <stephan.meijer@gmail.com>
- Loading branch information
Showing
8 changed files
with
153 additions
and
0 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
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: AMap | ||
menu: Providers | ||
route: /providers/amap | ||
--- | ||
|
||
import Playground from '../components/Playground'; | ||
import Map from '../components/Map'; | ||
|
||
# AMap Provider | ||
|
||
**note**: AMap services require an API key. [Obtain here][1]. | ||
For more options and configurations, see the [AMap developer docs][2]. | ||
|
||
<Playground> | ||
<Map provider="AMap" /> | ||
</Playground> | ||
|
||
```js | ||
import { AMapProvider } from 'leaflet-geosearch'; | ||
|
||
const provider = new AMapProvider({ | ||
params: { | ||
key: '__YOUR_AMAP_KEY__', | ||
}, | ||
}); | ||
|
||
// add to leaflet | ||
import { GeoSearchControl } from 'leaflet-geosearch'; | ||
|
||
map.addControl( | ||
new GeoSearchControl({ | ||
provider, | ||
style: 'bar', | ||
}), | ||
); | ||
``` | ||
|
||
[1]: https://console.amap.com/dev/key/app | ||
[2]: https://lbs.amap.com/api/webservice/guide/api/georegeo |
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,24 @@ | ||
import Provider from '../amapProvider'; | ||
import fixtures from './amapResponse.json'; | ||
|
||
describe('AMapProvider', () => { | ||
beforeAll(() => { | ||
fetch.mockResponse(async () => ({ body: JSON.stringify(fixtures) })); | ||
}); | ||
|
||
test('Can fetch results', async () => { | ||
const provider = new Provider({ | ||
params: { | ||
key: process.env.AMAP_API_KEY, | ||
}, | ||
}); | ||
|
||
const results = await provider.search({ query: '上海市浦东新区外滩' }); | ||
const result = results[0]; | ||
|
||
expect(result.label).toBeTruthy(); | ||
expect(`${result.x},${result.y}`).toEqual(fixtures.geocodes[0].location); | ||
// amap provider doesn't return bounds :( | ||
expect(result.bounds).toBeNull(); | ||
}); | ||
}); |
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,30 @@ | ||
{ | ||
"status": "1", | ||
"info": "OK", | ||
"infocode": "10000", | ||
"count": "1", | ||
"geocodes": [ | ||
{ | ||
"formatted_address": "上海市浦东新区外滩", | ||
"country": "中国", | ||
"province": "上海市", | ||
"citycode": "021", | ||
"city": "上海市", | ||
"district": "浦东新区", | ||
"township": [], | ||
"neighborhood": { | ||
"name": [], | ||
"type": [] | ||
}, | ||
"building": { | ||
"name": [], | ||
"type": [] | ||
}, | ||
"adcode": "310115", | ||
"street": [], | ||
"number": [], | ||
"location": "121.497253,31.238235", | ||
"level": "住宅区" | ||
} | ||
] | ||
} |
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,51 @@ | ||
import AbstractProvider, { | ||
EndpointArgument, | ||
ParseArgument, | ||
SearchResult, | ||
} from './provider'; | ||
|
||
export type RequestResult = { | ||
status: number; | ||
count: number; | ||
info: string; | ||
geocodes: RawResult[]; | ||
}; | ||
|
||
export interface RawResult { | ||
formatted_address: string; | ||
country: string; | ||
province: string; | ||
city: string; | ||
citycode: string; | ||
district: string; | ||
number: string; | ||
adcode: string; | ||
location: string; | ||
level: string; | ||
} | ||
|
||
export default class AMapProvider extends AbstractProvider< | ||
RequestResult, | ||
RawResult | ||
> { | ||
searchUrl = 'https://restapi.amap.com/v3/geocode/geo'; | ||
|
||
endpoint({ query }: EndpointArgument): string { | ||
const params = typeof query === 'string' ? { address: query } : query; | ||
params.output = 'JSON'; | ||
|
||
return this.getUrl(this.searchUrl, params); | ||
} | ||
|
||
parse(response: ParseArgument<RequestResult>): SearchResult<RawResult>[] { | ||
const records = response.data.geocodes ?? []; | ||
|
||
return records.map((r) => ({ | ||
x: Number(r.location.substring(0, r.location.indexOf(','))), | ||
y: Number(r.location.substring(r.location.indexOf(',') + 1)), | ||
label: r.formatted_address, | ||
bounds: null, | ||
raw: r, | ||
})); | ||
} | ||
} |