forked from OfficeDev/M365Bootcamp-TeamsEmergencyResponse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBingMapsService.ts
40 lines (34 loc) · 1.44 KB
/
BingMapsService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { IBingMapsService, IBingMapsServiceProps } from './IBingMapsService';
import IGetLocationsResponse from './WebServicePayloads/IGetLocationsResponse';
export default class BingMapsService implements IBingMapsService {
private credentials;
constructor(props: IBingMapsServiceProps) {
this.credentials = props.credentials;
}
public async geoCode(countryRegion: string,
adminDistrict: string,
locality: string,
address: string
): Promise<{latitude: number, longitude: number} | string> {
const url = `https://dev.virtualearth.net/REST/v1/Locations?countryRegion=${countryRegion}&adminDistrict=${adminDistrict}&locality=${locality}&postalCode=-&addressLine=${address}&maxResults=1&key=${this.credentials}`;
try {
const response = await fetch(url, {
method: 'GET',
headers: { "accept": "application/json" },
mode: 'cors',
cache: 'default'
});
if (response.ok) {
const value: IGetLocationsResponse = await response.json();
const coordinates = value.resourceSets[0].resources[0].point.coordinates;
return ({
latitude: coordinates[0],
longitude: coordinates[1]
});
}
}
catch (error) {
return error;
}
}
}