تقنية

صيغ خرائط Google لأوراق Google


يمكنك إحضار قوة خرائط Google إلى أوراق Google باستخدام صيغ بسيطة بدون ترميز. لا تحتاج إلى الاشتراك في واجهة برمجة تطبيقات Google Maps وجميع النتائج من خرائط Google في الورقة ، لذا فمن غير المرجح أن تضغط على أي حدود للحصة.

لإعطائك مثالًا سريعًا ، إذا كان لديك عنوان البداية في العمود A وعنوان الوجهة في العمود B ، مثل صيغة مثل =GOOGLEMAPS_DISTANCE(A1, B1, "driving") سوف يحسب بسرعة المسافة بين النقطتين.

أو تعديل الصيغة قليلاً =GOOGLEMAPS_TIME(A1, B1, "walking") لمعرفة المدة التي سيستغرقها الشخص للمشي من نقطة إلى أخرى.

إذا كنت ترغب في تجربة صيغ خرائط Google دون الدخول في التفاصيل الفنية ، فما عليك سوى إنشاء نسخة من ورقة Google هذه وأنك يتم تعيينك جميعًا.

استخدام خرائط Google داخل أوراق Google

يشرح هذا البرنامج التعليمي كيف يمكنك بسهولة كتابة وظائف خرائط Google المخصصة داخل أوراق Google التي ستساعدك:

  1. حساب المسافات بين مدينتين أو أي عناوين.

  2. احسب وقت السفر (المشي أو القيادة أو ركوب الدراجات) بين نقطتين.
  3. احصل على تحسس خطوط الطول والعرض لأي عنوان على خرائط Google.
  4. استخدم الترميز الجغرافي العكسي للعثور على العنوان البريدي من تحسس GPS.
  5. طباعة اتجاهات القيادة بين أي نقاط على الأرض.
  6. احصل على العنوان من الرمز البريدي نفسه.

1. حساب المسافات في أوراق Google

حدد الأصل ، والوجهة ، ووضع السفر (المشي أو القيادة) ، وستعود الوظيفة إلى المسافة بين النقطتين بالأميال.

=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

حدد خط العرض والطول واحصل على العنوان الكامل للنقطة من خلال الترميز الجغرافي العكسي للإحداثيات.

=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. احصل على إحداثيات GPS للعنوان

احصل على خط العرض وخط الطول لأي عنوان على خرائط Google.

=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. طباعة اتجاهات القيادة بين العناوين

حدد عنوان الأصل وعنوان الوجهة ووضع السفر والوظيفة سيستخدم واجهة برمجة تطبيقات خرائط Google لطباعة اتجاهات القيادة خطوة بخطوة.

=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

حدد عنوان المنشأ وعنوان الوجهة ووضع السفر والوظيفة سيقيس وقت رحلتك التقريبية بين العناوين المحددة ، شريطة وجود مسار.

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

وظائف خرائط Google في الأوراق

نصيحة: تحسين أداء الصيغة عن طريق التخزين المؤقت

تعمل Google Sheets داخليًا استخدام واجهة برمجة تطبيقات خرائط Google لحساب الطرق والمسافات ووقت السفر. تقدم Google حصصًا محدودة لعمليات الخرائط وإذا كانت الورقة تقوم بالكثير من الاستفسارات في فترة قصيرة ، فمن المحتمل أن ترى أخطاء مثل “” الخدمة التي تم استدعاؤها عدة مرات ليوم واحد “أو شيء مشابه.

للتغلب على مشكلة الحصص ، يوصى باستخدام ذاكرة التخزين المؤقت المدمجة لـ Apps Script لتخزين النتائج ، وإذا كانت نتائج وظيفة موجودة بالفعل في الحالة ، فستقوم بتقديم طلب أقل لخرائط Google.

تعمل الخرائط داخل ورقة Google هذه أيضًا التخزين المؤقت ، وإليك كيف يمكنك تنفيذها.

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

انظر أيضًا: ins ins in in expels and documents



Source link


اكتشاف المزيد من مرابع التكنولوجيا

اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى

اكتشاف المزيد من مرابع التكنولوجيا

اشترك الآن للاستمرار في القراءة والحصول على حق الوصول إلى الأرشيف الكامل.

Continue reading