Rumus Google Maps untuk Google Spreadsheet

Diterbitkan: 2022-02-26

Anda dapat menghadirkan kecanggihan Google Maps ke Google Spreadsheet Anda menggunakan rumus sederhana tanpa pengkodean. Anda tidak perlu mendaftar ke Google Maps API dan semua hasil dari Google Maps di-cache di lembar sehingga Anda tidak akan mencapai batas kuota apa pun.

Sebagai contoh singkat, jika Anda memiliki alamat awal di kolom A dan alamat tujuan di kolom B, rumus seperti =GOOGLEMAPS_DISTANCE(A1, B1, "driving") akan dengan cepat menghitung jarak antara dua titik.

Atau ubah sedikit rumus =GOOGLEMAPS_TIME(A1, B1, "walking") untuk mengetahui berapa lama waktu yang dibutuhkan seseorang untuk berjalan dari satu titik ke titik lainnya.

Jika Anda ingin mencoba rumus Google Maps tanpa masuk ke detail teknis, cukup buat salinan Google Sheet ini dan Anda sudah siap.

Google Maps in Google Sheets

Menggunakan Google Maps di dalam Google Spreadsheet

Tutorial ini menjelaskan bagaimana Anda dapat dengan mudah menulis fungsi Google Maps kustom di dalam Google Spreadsheet yang akan membantu Anda:

  1. Hitung jarak antara dua kota atau alamat mana pun.
  2. Hitung waktu tempuh (berjalan, mengemudi atau bersepeda) antara dua titik.
  3. Dapatkan koordinat lintang dan bujur dari alamat mana pun di Google Maps.
  4. Gunakan geocoding terbalik untuk menemukan alamat pos dari koordinat GPS.
  5. Cetak petunjuk arah mengemudi di antara titik mana pun di bumi.
  6. Dapatkan alamat dari kode pos itu sendiri.

1. Hitung Jarak di Google Sheets

Tentukan asal, tujuan, mode perjalanan (berjalan atau mengemudi) dan fungsi akan mengembalikan jarak antara dua titik dalam mil.

=GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")

 /** * Calculate the distance between two * locations on Google Maps. * * =GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking") * * @param {String} origin The address of starting point * @param {String} destination The address of destination * @param {String} mode The mode of travel (driving, walking, bicycling or transit) * @return {String} The distance in miles * @customFunction */ const GOOGLEMAPS_DISTANCE = ( origin , destination , mode ) => { const { routes : [ data ] = [ ] } = Maps . newDirectionFinder ( ) . setOrigin ( origin ) . setDestination ( destination ) . setMode ( mode ) . getDirections ( ) ; if ( ! data ) { throw new Error ( 'No route found!' ) ; } const { legs : [ { distance : { text : distance } } = { } ] = [ ] } = data ; return distance ; } ;

2. Membalikkan Geocoding di Google Spreadsheet

Tentukan lintang dan bujur dan dapatkan alamat lengkap titik tersebut melalui geocoding terbalik koordinat.

=GOOGLEMAPS_DISTANCE("NY 10005", "Hoboken NJ", "walking")

 /** * Use Reverse Geocoding to get the address of * a point location (latitude, longitude) on Google Maps. * * =GOOGLEMAPS_REVERSEGEOCODE(latitude, longitude) * * @param {String} latitude The latitude to lookup. * @param {String} longitude The longitude to lookup. * @return {String} The postal address of the point. * @customFunction */ const GOOGLEMAPS_REVERSEGEOCODE = ( latitude , longitude ) => { const { results : [ data = { } ] = [ ] } = Maps . newGeocoder ( ) . reverseGeocode ( latitude , longitude ) ; return data . formatted_address ; } ;

3. Dapatkan koordinat GPS dari sebuah alamat

Dapatkan garis lintang dan bujur dari setiap alamat di Google Maps.

=GOOGLEMAPS_LATLONG("10 Hanover Square, NY")

 /** * Get the latitude and longitude of any * address on Google Maps. * * =GOOGLEMAPS_LATLONG("10 Hanover Square, NY") * * @param {String} address The address to lookup. * @return {String} The latitude and longitude of the address. * @customFunction */ const GOOGLEMAPS_LATLONG = ( address ) => { const { results : [ data = null ] = [ ] } = Maps . newGeocoder ( ) . geocode ( address ) ; if ( data === null ) { throw new Error ( 'Address not found!' ) ; } const { geometry : { location : { lat , lng } } = { } } = data ; return ` ${ lat } , ${ lng } ` ; } ;

4. Cetak petunjuk arah antar alamat

Tentukan alamat asal, alamat tujuan, mode perjalanan dan fungsi yang akan menggunakan Google Maps API untuk mencetak petunjuk arah mengemudi langkah demi langkah.

=GOOGLEMAPS_DIRECTIONS("NY 10005", "Hoboken NJ", "walking")

 /** * Find the driving direction between two * locations on Google Maps. * * =GOOGLEMAPS_DIRECTIONS("NY 10005", "Hoboken NJ", "walking") * * @param {String} origin The address of starting point * @param {String} destination The address of destination * @param {String} mode The mode of travel (driving, walking, bicycling or transit) * @return {String} The driving direction * @customFunction */ const GOOGLEMAPS_DIRECTIONS = ( origin , destination , mode = 'driving' ) => { const { routes = [ ] } = Maps . newDirectionFinder ( ) . setOrigin ( origin ) . setDestination ( destination ) . setMode ( mode ) . getDirections ( ) ; if ( ! routes . length ) { throw new Error ( 'No route found!' ) ; } return routes . map ( ( { legs } ) => { return legs . map ( ( { steps } ) => { return steps . map ( ( step ) => { return step . html_instructions . replace ( / <[^>]+> / g , '' ) ; } ) ; } ) ; } ) . join ( ', ' ) ; } ;

5. Ukur waktu perjalanan dengan Google Maps

Tentukan alamat asal, alamat tujuan, mode perjalanan dan fungsi akan mengukur perkiraan waktu perjalanan Anda antara alamat yang ditentukan, asalkan ada rute.

=GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking")

 /** * Calculate the travel time between two locations * on Google Maps. * * =GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking") * * @param {String} origin The address of starting point * @param {String} destination The address of destination * @param {String} mode The mode of travel (driving, walking, bicycling or transit) * @return {String} The time in minutes * @customFunction */ const GOOGLEMAPS_DURATION = ( origin , destination , mode = 'driving' ) => { const { routes : [ data ] = [ ] } = Maps . newDirectionFinder ( ) . setOrigin ( origin ) . setDestination ( destination ) . setMode ( mode ) . getDirections ( ) ; if ( ! data ) { throw new Error ( 'No route found!' ) ; } const { legs : [ { duration : { text : time } } = { } ] = [ ] } = data ; return time ; } ;

Fungsi Google Maps di Spreadsheet

Tip: Tingkatkan Performa dengan Hasil Caching

Semua fungsi Google Spreadsheet di atas secara internal menggunakan Google Maps API untuk menghitung rute, jarak, dan waktu perjalanan. Google menawarkan kuota terbatas untuk operasi Maps dan jika sheet Anda melakukan terlalu banyak kueri dalam waktu singkat, Anda mungkin akan melihat kesalahan seperti ""Layanan dipanggil terlalu banyak untuk satu hari" atau yang serupa.

Untuk mengatasi masalah ini, disarankan agar Anda menggunakan cache bawaan Apps Script untuk menyimpan hasil dan, jika hasil fungsi sudah ada dalam kasus ini, Anda akan mengurangi satu permintaan ke Google Maps. Fungsi Maps di dalam ini Google Sheet juga menggunakan caching dan inilah cara Anda menerapkannya.

 // The cache key for "New York" and "new york " should be same const md5 = ( key = '' ) => { const code = key . toLowerCase ( ) . replace ( / \s / g , '' ) ; return Utilities . computeDigest ( Utilities . DigestAlgorithm . MD5 , key ) . map ( ( char ) => ( char + 256 ) . toString ( 16 ) . slice ( - 2 ) ) . join ( '' ) ; } ; const getCache = ( key ) => { return CacheService . getDocumentCache ( ) . get ( md5 ( key ) ) ; } ; // Store the results for 6 hours const setCache = ( key , value ) => { const expirationInSeconds = 6 * 60 * 60 ; CacheService . getDocumentCache ( ) . put ( md5 ( key ) , value , expirationInSeconds ) ; } ; /** * Calculate the travel time between two locations * on Google Maps. * * =GOOGLEMAPS_DURATION("NY 10005", "Hoboken NJ", "walking") * * @param {String} origin The address of starting point * @param {String} destination The address of destination * @param {String} mode The mode of travel (driving, walking, bicycling or transit) * @return {String} The time in minutes * @customFunction */ const GOOGLEMAPS_DURATION = ( origin , destination , mode = 'driving' ) => { const key = [ 'duration' , origin , destination , mode ] . join ( ',' ) ; // Is result in the internal cache? const value = getCache ( key ) ; // If yes, serve the cached result if ( value !== null ) return value ; const { routes : [ data ] = [ ] } = Maps . newDirectionFinder ( ) . setOrigin ( origin ) . setDestination ( destination ) . setMode ( mode ) . getDirections ( ) ; if ( ! data ) { throw new Error ( 'No route found!' ) ; } const { legs : [ { duration : { text : time } } = { } ] = [ ] } = data ; // Store the result in internal cache for future setCache ( key , time ) ; return time ; } ;

Lihat juga: Menyematkan Google Maps di Email dan Dokumen