@rr0/place
Version:
Place representation API
59 lines (58 loc) • 2.71 kB
JavaScript
import { Client, Status } from "@googlemaps/google-maps-services-js";
import { PlaceService } from "./PlaceService.js";
import { PlaceLocation } from "./location/PlaceLocation.js";
export class GooglePlaceService extends PlaceService {
constructor(rootDir, apiKey) {
super(rootDir);
this.apiKey = apiKey;
this.client = new Client({});
}
async getElevation(location) {
console.debug("Looking for elevation of", location);
const elevationResponse = await this.client.elevation({ params: { locations: [location], key: this.apiKey } });
const data = elevationResponse.data;
switch (data.status) {
case Status.OK:
const results = data.results;
switch (results.length) {
case 0:
throw Error(`No results when looking for elevation of ${JSON.stringify(location)}`);
case 1:
const result = results[0];
return { elevation: result.elevation, data: result };
default:
throw Error(`More than 1 result when looking for elevation of ${JSON.stringify(location)}`);
}
case Status.NOT_FOUND:
case Status.ZERO_RESULTS:
break;
default:
throw Error(`Unexpected status ${data.status} when looking for elevation of ${JSON.stringify(location)}: ${data.error_message}`);
}
}
async geocode(address) {
console.debug("Geocoding", address);
const response = await this.client.geocode({ params: { address, key: this.apiKey } });
const data = response.data;
switch (data.status) {
case Status.OK:
const results = data.results;
switch (results.length) {
case 0:
throw Error(`No results when geocoding "${address}"`);
case 1:
const singleResult = results[0];
const locationData = singleResult.geometry.location;
const location = new PlaceLocation(locationData.lat, locationData.lng);
return { location, data: singleResult };
default:
throw Error(`More than 1 result when geocoding "${address}": ${JSON.stringify(results)}`);
}
case Status.NOT_FOUND:
case Status.ZERO_RESULTS:
break;
default:
throw Error(`Unexpected status ${data.status} when geocoding "${address}": ${data.error_message}`);
}
}
}