Skip to content

Commit fe6d699

Browse files
author
Sourabh Choraria
committed
added Maps functions by labnol
1 parent aefb825 commit fe6d699

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

functions/GOOGLEMAPS_DIRECTIONS.gs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @author Amit Agarwal https://www.labnol.org/google-maps-sheets-200817
2+
/**
3+
* Find the driving direction between two
4+
* locations on Google Maps.
5+
*
6+
* =GOOGLEMAPS_DIRECTIONS("NY 10005", "Hoboken NJ", "walking")
7+
*
8+
* @param {String} origin The address of starting point
9+
* @param {String} destination The address of destination
10+
* @param {String} mode The mode of travel (driving, walking, bicycling or transit)
11+
* @return {String} The driving direction
12+
* @customFunction
13+
*/
14+
const GOOGLEMAPS_DIRECTIONS = (origin, destination, mode = 'driving') => {
15+
const { routes = [] } = Maps.newDirectionFinder()
16+
.setOrigin(origin)
17+
.setDestination(destination)
18+
.setMode(mode)
19+
.getDirections();
20+
if (!routes.length) {
21+
throw new Error('No route found!');
22+
}
23+
return routes
24+
.map(({ legs }) => {
25+
return legs.map(({ steps }) => {
26+
return steps.map((step) => {
27+
return step.html_instructions.replace(/<[^>]+>/g, '');
28+
});
29+
});
30+
})
31+
.join(', ');
32+
};

functions/GOOGLEMAPS_DISTANCE.gs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @author Amit Agarwal https://www.labnol.org/google-maps-sheets-200817
22
/**
3-
* Calculate the distance between two locations on Google Maps.
3+
* Calculate the distance between two
4+
* locations on Google Maps.
45
*
56
* =GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")
67
*

functions/GOOGLEMAPS_DURATION.gs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// @author Amit Agarwal https://www.labnol.org/google-maps-sheets-200817
2+
/**
3+
* Calculate the travel time between two locations
4+
* on Google Maps.
5+
*
6+
* =GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking")
7+
*
8+
* @param {String} origin The address of starting point
9+
* @param {String} destination The address of destination
10+
* @param {String} mode The mode of travel (driving, walking, bicycling or transit)
11+
* @return {String} The time in minutes
12+
* @customFunction
13+
*/
14+
const GOOGLEMAPS_DURATION = (origin, destination, mode = 'driving') => {
15+
const { routes: [data] = [] } = Maps.newDirectionFinder()
16+
.setOrigin(origin)
17+
.setDestination(destination)
18+
.setMode(mode)
19+
.getDirections();
20+
if (!data) {
21+
throw new Error('No route found!');
22+
}
23+
const { legs: [{ duration: { text: time } } = {}] = [] } = data;
24+
return time;
25+
};

functions/GOOGLEMAPS_LATLONG.gs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @author Amit Agarwal https://www.labnol.org/google-maps-sheets-200817
2+
/**
3+
* Get the latitude and longitude of any
4+
* address on Google Maps.
5+
*
6+
* =GOOGLEMAPS_LATLONG("10 Hanover Square, NY")
7+
*
8+
* @param {String} address The address to lookup.
9+
* @return {String} The latitude and longitude of the address.
10+
* @customFunction
11+
*/
12+
const GOOGLEMAPS_LATLONG = (address) => {
13+
const { results: [data = null] = [] } = Maps.newGeocoder().geocode(address);
14+
if (data === null) {
15+
throw new Error('Address not found!');
16+
}
17+
const { geometry: { location: { lat, lng } } = {} } = data;
18+
return `${lat}, ${lng}`;
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @author Amit Agarwal https://www.labnol.org/google-maps-sheets-200817
2+
/**
3+
* Use Reverse Geocoding to get the address of
4+
* a point location (latitude, longitude) on Google Maps.
5+
*
6+
* =GOOGLEMAPS_REVERSEGEOCODE(latitude, longitude)
7+
*
8+
* @param {String} latitude The latitude to lookup.
9+
* @param {String} longitude The longitude to lookup.
10+
* @return {String} The postal address of the point.
11+
* @customFunction
12+
*/
13+
const GOOGLEMAPS_REVERSEGEOCODE = (latitude, longitude) => {
14+
const { results: [data = {}] = [] } = Maps.newGeocoder().reverseGeocode(latitude, longitude);
15+
return data.formatted_address;
16+
};

0 commit comments

Comments
 (0)