31 lines
870 B
TypeScript
31 lines
870 B
TypeScript
import ILocation from '@/models/ILocation'
|
|
import ILocationQuery from '@/models/ILocationQuery'
|
|
|
|
class MapService {
|
|
private key: string = process.env.VUE_APP_MAP_KEY || ''
|
|
|
|
public async getLocation(location: ILocation): Promise<string> {
|
|
const result = await fetch(this.url(location.lat, location.lon))
|
|
if (!result) {
|
|
return ''
|
|
}
|
|
const json: ILocationQuery = await result.json()
|
|
if (
|
|
!json ||
|
|
!json.resourceSets ||
|
|
!json.resourceSets.length ||
|
|
!json.resourceSets[0].resources.length
|
|
) {
|
|
return ''
|
|
}
|
|
const address = json.resourceSets[0].resources[0].address
|
|
return address ? address.locality : ''
|
|
}
|
|
|
|
private url(latitude: number, longitude: number) {
|
|
return `https://dev.virtualearth.net/REST/v1/Locations/${latitude},${longitude}?key=${this.key}`
|
|
}
|
|
}
|
|
|
|
export default new MapService()
|