การใช้ Google Apps Script เพื่อดึงข้อมูลตำแหน่งโดยใช้ Maps.newGeocoder()
Google Apps Script เป็นเครื่องมือที่ช่วยให้เราสามารถพัฒนาโปรแกรมเสริม (Add-ons) หรือสร้างแอปพลิเคชันที่ใช้งานร่วมกับ Google Workspace ได้อย่างง่ายดาย หนึ่งในฟีเจอร์ที่มีประโยชน์มากคือการทำงานกับแผนที่และตำแหน่งโดยใช้ Maps.newGeocoder()
สำหรับการแปลงพิกัด (Latitude, Longitude) เป็นที่อยู่จริง (Reverse Geocoding)
ฟังก์ชัน Maps.newGeocoder()
ฟังก์ชันนี้เป็นส่วนหนึ่งของ Google Maps Services ใน Google Apps Script ที่ช่วยให้คุณสามารถ:
- แปลงพิกัดตำแหน่งเป็นข้อมูลที่อยู่ (Reverse Geocoding)
- ปรับแต่งผลลัพธ์ เช่น ภูมิภาคและภาษา
ตัวอย่างการใช้งาน Maps.newGeocoder()
ดึงข้อมูลที่อยู่จากพิกัดตำแหน่ง
ฟังก์ชันตัวอย่างด้านล่างจะรับค่าพิกัด ละติจูด (Latitude) และ ลองจิจูด (Longitude) และส่งคืนข้อมูลที่อยู่ในรูปแบบข้อความ (Formatted Address) พร้อมระบุชื่อเมืองและประเทศ
function getLocationDetails(latitude, longitude) { // ใช้ Maps.newGeocoder เพื่อดึงข้อมูล Reverse Geocoding const response = Maps.newGeocoder() .setRegion('en') // กำหนดภูมิภาค (เช่น 'en' สำหรับภาษาอังกฤษ) .setLanguage('en') // กำหนดภาษา .reverseGeocode(latitude, longitude); // ดึงข้อมูลที่อยู่จากพิกัด // ตรวจสอบผลลัพธ์ if (response.results && response.results.length > 0) { const formattedAddress = response.results[0].formatted_address; // ที่อยู่เต็ม const addressComponents = response.results[0].address_components; let city = ''; let country = ''; // วนลูปเพื่อดึงข้อมูลชื่อเมืองและประเทศ addressComponents.forEach(component => { if (component.types.includes('locality')) { city = component.long_name; // เมือง } if (component.types.includes('country')) { country = component.long_name; // ประเทศ } }); return { city, country, fullAddress: formattedAddress }; // คืนค่าข้อมูล } else { throw new Error('ไม่พบข้อมูลตำแหน่งสำหรับพิกัดที่กำหนด'); } } // ฟังก์ชันทดสอบ function testGetLocationDetails() { const latitude = 13.7563; // ตัวอย่างพิกัด (กรุงเทพฯ) const longitude = 100.5018; try { const location = getLocationDetails(latitude, longitude); Logger.log(`City: ${location.city}, Country: ${location.country}, Full Address: ${location.fullAddress}`); } catch (e) { Logger.log(`Error: ${e.message}`); } }
อธิบายการทำงาน
1. Maps.newGeocoder()
- เรียกใช้ฟังก์ชันเพื่อทำงานกับ Geocoding
- สามารถปรับภูมิภาค (
setRegion()
) และภาษา (setLanguage()
) ได้
2. reverseGeocode(latitude, longitude)
- แปลงพิกัดตำแหน่งเป็นข้อมูลที่อยู่โดยละเอียด
3. ดึงข้อมูลเมืองและประเทศ
- ใช้
address_components
เพื่อระบุส่วนประกอบของที่อยู่ เช่น เมือง (locality
) และประเทศ (country
)
ผลลัพธ์ที่ได้
เมื่อรันฟังก์ชัน testGetLocationDetails
คุณจะได้ข้อมูลที่อยู่ในรูปแบบ:
ประโยชน์ของการใช้ Maps.newGeocoder()
- สะดวก: ไม่ต้องตั้งค่า Google Maps API Key เพิ่มเติม
- ประหยัดเวลา: ใช้งานง่ายด้วยโค้ดเพียงไม่กี่บรรทัด
- ความแม่นยำ: ข้อมูลที่ได้รับจาก Google Maps มีความน่าเชื่อถือและอัปเดตเสมอ
การนำไปใช้งาน
ฟังก์ชันนี้เหมาะสำหรับ:
- ระบบติดตามตำแหน่ง (Location Tracking)
- ระบบแจ้งที่อยู่ (Address Notification) เช่น แอปพลิเคชันจัดส่งสินค้า
- การบันทึกข้อมูลตำแหน่งผู้ใช้ในฟอร์มหรือฐานข้อมูล
หมายเหตุ
- การใช้
Maps.newGeocoder()
มีข้อจำกัดเรื่อง โควต้า (Quota) ตรวจสอบเพิ่มเติมได้ที่ Google Apps Script Quotas. - หากต้องการปรับแต่งข้อมูลเพิ่มเติม เช่น รูปแบบที่อยู่ หรือภาษา สามารถปรับค่าใน
setRegion()
และsetLanguage()
ได้ตามความต้องการ