diff --git a/e2e/elevation.test.ts b/e2e/elevation.test.ts index 9bcd400dca..6b695a4045 100644 --- a/e2e/elevation.test.ts +++ b/e2e/elevation.test.ts @@ -13,3 +13,20 @@ test("elevation should return correct result", async () => { expect(r.data.results.length).toEqual(2); }); + +test("elevation should return correct result with path params", async () => { + const path = [ + { lat: 35, lng: -110 }, + { lat: 45, lng: -110 } + ]; + + const params = { + path: path, + samples: 10, + key: process.env.GOOGLE_MAPS_API_KEY + }; + + const r = await elevation({ params: params }); + + expect(r.data.results.length).toEqual(10); +}); diff --git a/src/elevation.test.ts b/src/elevation.test.ts index 5cd092efd0..c168f022b3 100644 --- a/src/elevation.test.ts +++ b/src/elevation.test.ts @@ -9,7 +9,7 @@ afterEach(() => { jest.clearAllMocks(); }); -test("elevation should call axios correctly", () => { +test("elevation should call axios correctly with location params", () => { const params = { locations: ["10,20"], key: "foo" }; elevation({ params: params }, mockedAxios); @@ -22,3 +22,24 @@ test("elevation should call axios correctly", () => { url: defaultUrl }); }); + +test("elevation should call axios correctly with path params", () => { + const params = { + path: [ + { lat: 35, lng: -110 }, + { lat: 45, lng: -110 } + ], + samples: 10, + key: "foo" + }; + + elevation({ params: params }, mockedAxios); + + expect(mockedAxios).toHaveBeenCalledTimes(1); + expect(mockedAxios).toHaveBeenCalledWith({ + method: "get", + params: params, + paramsSerializer: defaultParamsSerializer, + url: defaultUrl + }); +}); diff --git a/src/elevation.ts b/src/elevation.ts index 54e0060acd..7880478708 100644 --- a/src/elevation.ts +++ b/src/elevation.ts @@ -3,15 +3,32 @@ import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; import { defaultAxiosInstance } from "./client"; import { serializer, latLngToString } from "./serialize"; +export interface PositionalElevationParams extends RequestParams { + /** + * defines the location(s) on the earth from which to return elevation data. + * This parameter takes either a single location as a comma-separated {latitude,longitude} pair (e.g. "40.714728,-73.998672") + * or multiple latitude/longitude pairs passed as an array or as an encoded polyline. + */ + locations: LatLng[]; +} + +export interface SampledPathElevationParams extends RequestParams { + /** + * defines a path on the earth for which to return elevation data. This parameter defines a + * set of two or more ordered pairs defining a path along the surface of the earth. This + * parameter must be used in conjunction with the samples parameter described below. + */ + path: LatLng[]; + /** + * specifies the number of sample points along a path for which to return elevation data. + * The samples parameter divides the given path into an ordered set of equidistant points + * along the path. + */ + samples: number; +} + export interface ElevationRequest extends Partial { - params: { - /** - * defines the location(s) on the earth from which to return elevation data. - * This parameter takes either a single location as a comma-separated {latitude,longitude} pair (e.g. "40.714728,-73.998672") - * or multiple latitude/longitude pairs passed as an array or as an encoded polyline. - */ - locations: LatLng[]; - } & RequestParams; + params: PositionalElevationParams | SampledPathElevationParams; } export interface ElevationResponseData extends ResponseData { results: { @@ -39,7 +56,8 @@ export interface ElevationResponse extends AxiosResponse { export const defaultUrl = "https://maps.googleapis.com/maps/api/elevation/json"; export const defaultParamsSerializer = serializer({ - locations: o => o.map(latLngToString) + locations: o => o.map(latLngToString), + path: o => o.map(latLngToString) }); export function elevation(