Skip to content

Commit

Permalink
feat: add support for elevation along path (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Mar 11, 2020
1 parent b037d3f commit 0a56948
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
17 changes: 17 additions & 0 deletions e2e/elevation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
23 changes: 22 additions & 1 deletion src/elevation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
});
});
36 changes: 27 additions & 9 deletions src/elevation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AxiosRequestConfig> {
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: {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0a56948

Please sign in to comment.