Skip to content

Instantly share code, notes, and snippets.

@lukas-zech-software
Last active April 9, 2021 21:56
Show Gist options
  • Save lukas-zech-software/a7e4a23a6833ec1abb1fc836138f7822 to your computer and use it in GitHub Desktop.
Save lukas-zech-software/a7e4a23a6833ec1abb1fc836138f7822 to your computer and use it in GitHub Desktop.
Typescript Definitions for official GoogleMaps API for Node.js (https://github.com/googlemaps/google-maps-services-js)
import { createClient, GoogleMapsClient } from '@google/maps';
export class GeoCodingService {
private geoCoder: GoogleMapsClient;
public constructor() {
this.geoCoder = createClient({
key: 'YOUR-API-KEY',
});
}
public geocodeAddress(): Promise<google.maps.GeocoderResult[]> {
const request: google.maps.GeocoderRequest = {
address: '1600 Amphitheatre Parkway, Mountain View, CA'
};
return new Promise<google.maps.GeocoderResult[]>((resolve, reject) => {
this.geoCoder.geocode(request, (error, response) => {
if (error) {
reject(error);
}
resolve(response.json.results);
});
});
}
}
// USAGE
new GeoCodingService().geocodeAddress().then((results) => {
console.log('results', results);
const result = response.json.results[0],
location = result.geometry.location;
// @types/googlemaps describe the Javascript API not the JSON object on the response
// there a sublte difference like lat/lng beeing number not functions, making this `<any>` cast necessary
resolve({
lat: <any>location.lat,
lng: <any>location.lng,
address: result.formatted_address,
});
});
declare module "@google/maps" {
export interface CreateClientOptions {
/**
* API key (required, unless clientID and
* clientSecret provided).
*/
key: string;
/**
* Maps API for Work client ID.
*/
clientId?: string;
/**
* Maps API for Work client secret (a.k.a. private key).
*/
clientSecret?: string;
/**
* Maps API for Work channel.
*/
channel?: string;
/**
* Timeout in milliseconds. (Default: 60 * 1000 ms)
*/
timeout?: number;
/**
* Promise constructor (optional).
* @constructor
*/
Promise?: new<T>() => PromiseLike<T>;
rate?: RateOptions;
retryOptions?: RetryOptions;
}
export interface RateOptions {
/**
* Controls rate-limiting of requests. Maximum number of requests per period.
* (Default: 10)
*/
limit: number;
/**
* Period for rate limit, in milliseconds. (Default: 1000 ms)
*/
period: number;
}
export interface RetryOptions {
/**
* If a transient server error
* occurs, how long to wait before retrying the request, in milliseconds.
* (Default: 500 ms)
*/
interval: number;
}
export type MapApiResponseHandler<T> = (error: any, response: MapApiResponse<T>) => void;
/**
* HTTP Response of the API call
*/
export interface MapApiResponse<T> {
/**
* HTTP Status Code
*/
status: number;
/**
* HTTP Header object
*/
headers: { [index: string]: string };
/**
* Payload of the API call
*/
json: T;
}
/**
* Payload of a Geocode response
*/
interface GeoCodeResponsePayload {
results: google.maps.GeocoderResult[];
status: google.maps.GeocoderStatus;
}
export class GoogleMapsClient {
/**
* Call to the Geocode API
*/
public geocode(request: google.maps.GeocoderRequest, callback: MapApiResponseHandler<GeoCodeResponsePayload>): void;
}
export function createClient(options: CreateClientOptions): GoogleMapsClient;
}
@ComFreek
Copy link

ComFreek commented Jan 1, 2018

Thanks you very much for providing this! I have added support for the Directions API: https://gist.github.com/ComFreek/c0476ffeb0c446728aadada920ebb61e

The issue in line 42 in your example above is really a bummer, unfortunately. I can't think of any way to solve it without copying @types/googlemaps all over and editing it (redundancy!). The DirectionsRequest object differs between the @google/maps API and the raw HTTP API as well, e.g. see https://gist.github.com/ComFreek/c0476ffeb0c446728aadada920ebb61e/#file-index-d-ts-L102, and DirectionsResult.status is a string, not an enum.

@lukaskopenec
Copy link

Thanks for providing this. For those who'd like to use promises their way (passing the promise constructor in the client options), you'll probably need to change the type at line 28 from Promise?: new<T>() => PromiseLike<T>; to Promise?: PromiseConstructorLike;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment