Google E-Tablolar için Google Haritalar Formülleri

Yayınlanan: 2022-02-26

Kodlama olmadan basit formüller kullanarak Google Haritalar'ın gücünü Google E-Tablolarınıza taşıyabilirsiniz. Google Haritalar API'sine kaydolmanıza gerek yoktur ve Google Haritalar'dan alınan tüm sonuçlar sayfada önbelleğe alınır, bu nedenle herhangi bir kota sınırına ulaşmanız olası değildir.

Size hızlı bir örnek vermek gerekirse, A sütununda başlangıç ​​adresiniz ve B sütununda hedef adresiniz varsa, =GOOGLEMAPS_DISTANCE(A1, B1, "driving") gibi bir formül iki nokta arasındaki mesafeyi hızla hesaplayacaktır.

Veya bir kişinin bir noktadan diğerine yürümesinin ne kadar süreceğini bilmek için formülü biraz değiştirin =GOOGLEMAPS_TIME(A1, B1, "walking") .

Google Haritalar formüllerini teknik ayrıntılara girmeden denemek isterseniz, bu Google E-Tablosunun bir kopyasını almanız yeterlidir.

Google Maps in Google Sheets

Google Haritalar'ı Google E-Tablolar'da Kullanma

Bu eğitici, size yardımcı olacak Google E-Tablolar içinde özel Google Haritalar işlevlerini nasıl kolayca yazabileceğinizi açıklar:

  1. İki şehir veya herhangi bir adres arasındaki mesafeleri hesaplayın.
  2. İki nokta arasındaki seyahat süresini (yürüme, araba sürme veya bisiklete binme) hesaplayın.
  3. Google Haritalar'daki herhangi bir adresin enlem ve boylam koordinatlarını alın.
  4. Posta adresini GPS koordinatlarından bulmak için ters coğrafi kodlamayı kullanın.
  5. Dünyadaki herhangi bir nokta arasında yol tarifi yazdırın.
  6. Adresi posta kodunun kendisinden alın.

1. Google E-Tablolarda Mesafeleri Hesaplayın

Kalkış noktasını, varış yerini, seyahat modunu (yürüme veya sürüş) belirtin ve işlev, iki nokta arasındaki mesafeyi mil cinsinden döndürür.

=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. Google E-Tablolarda Ters Geocoding

Enlem ve boylamı belirtin ve koordinatların ters coğrafi kodlamasıyla noktanın tam adresini alın.

=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. Bir adresin GPS koordinatlarını alın

Google Haritalar'daki herhangi bir adresin enlem ve boylamını alın.

=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. Adresler arasındaki yol tariflerini yazdırın

Başlangıç ​​adresini, varış adresini, seyahat modunu belirtin ve işlev, adım adım yol tariflerini yazdırmak için Google Haritalar API'sini kullanır.

=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. Google Haritalar ile yolculuk süresini ölçün

Başlangıç ​​adresini, varış adresini, seyahat modunu belirtin ve işlev, bir rota olması koşuluyla belirtilen adresler arasındaki yaklaşık seyahat sürenizi ölçecektir.

=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 ; } ;

E-Tablolardaki Google Haritalar İşlevleri

İpucu: Sonuçları Önbelleğe Alarak Performansı Artırın

Yukarıdaki tüm Google E-Tablolar işlevleri, rotaları, mesafeleri ve seyahat süresini hesaplamak için dahili olarak Google Haritalar API'sini kullanır. Google, Haritalar işlemleri için sınırlı bir kota sunar ve sayfanız kısa sürede çok fazla sorgu gerçekleştirirse, ""Hizmet bir gün içinde çok fazla kez çağrıldı" veya benzeri hatalar görmeniz olasıdır.

Bu sorunu aşmak için, sonuçları depolamak için Apps Komut Dosyası'nın yerleşik önbelleğini kullanmanız önerilir ve bu durumda bir işlevin sonuçları zaten mevcutsa, Google Haritalar'a bir daha az istekte bulunmanız önerilir. Google E-Tablosu ayrıca önbelleğe almayı kullanır ve bunu nasıl uygulayabileceğiniz aşağıda açıklanmıştır.

 // 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 ; } ;

Ayrıca bkz: Google Haritalar'ı E-postalara ve Belgelere Göm