คู่มือนี้อธิบายกลยุทธ์ต่างๆ ในการเพิ่มประสิทธิภาพการใช้งาน Google Maps API
ในด้านความปลอดภัย ประสิทธิภาพ และการใช้งาน
ความปลอดภัย
การตรวจสอบแนวทางปฏิบัติแนะนำด้านความปลอดภัย
คีย์ API เป็นข้อมูลเข้าสู่ระบบที่เน้นโปรเจ็กต์ซึ่งควรได้รับการป้องกัน
เช่นเดียวกับรหัสผู้ใช้และรหัสผ่าน อ่านแนวทางปฏิบัติแนะนำด้านความปลอดภัยของ API เพื่อรักษาคีย์ให้ปลอดภัยจากการใช้งานที่ไม่พึงประสงค์ ซึ่งอาจทำให้มีการใช้โควต้ามากเกินไปและมีการเรียกเก็บเงินที่ไม่คาดคิดในบัญชี
การใช้คีย์ API เพื่อเข้าถึง Maps API
คีย์ API เป็นวิธีการตรวจสอบสิทธิ์ที่แนะนำสำหรับการเข้าถึง
Google Maps API แม้ว่าระบบจะยังคงรองรับการใช้รหัสไคลเอ็นต์ แต่คีย์ API รองรับการควบคุมความปลอดภัยที่ละเอียดยิ่งขึ้น และสามารถปรับให้ทำงานกับที่อยู่เว็บ ที่อยู่ IP และ SDK สำหรับอุปกรณ์เคลื่อนที่ (Android และ iOS) ที่เฉพาะเจาะจงได้ ดูข้อมูลเกี่ยวกับการสร้างและรักษาความปลอดภัยของคีย์ API ได้ที่หน้า "การใช้คีย์ API" สำหรับ API หรือ SDK แต่ละรายการ (เช่น สำหรับ Maps JavaScript API ให้ไปที่หน้าการใช้คีย์ API)
ประสิทธิภาพ
การใช้ Exponential Backoff เพื่อจัดการข้อผิดพลาด
หากแอปของคุณพบข้อผิดพลาดจากการพยายามเรียก API มากเกินไปภายในระยะเวลาอันสั้น เช่น ข้อผิดพลาดเกี่ยวกับโควต้า ให้ลองใช้การถอยแบบทวีคูณเพื่อให้คำขอประมวลผล
Exponential Backoff มีประโยชน์มากที่สุดสำหรับข้อผิดพลาดในหมวด 500
คำขอไปยัง API ที่รวมการโต้ตอบของผู้ใช้ควรส่งเมื่อมีการร้องขอเท่านั้น
ซึ่งหมายถึงการรอให้ผู้ใช้ปลายทางดำเนินการ (เช่น on-click)
เพื่อเริ่มคำขอ API จากนั้นใช้ผลลัพธ์เพื่อโหลดแผนที่ ตั้งค่า
ปลายทาง หรือแสดงข้อมูลที่เหมาะสม การใช้วิธีการแบบออนดีมานด์
จะช่วยหลีกเลี่ยงคำขอที่ไม่จำเป็นไปยัง API ซึ่งจะช่วยลดการใช้ API
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThis guide provides strategies for optimizing Google Maps APIs usage, focusing on security, performance, and cost management.\u003c/p\u003e\n"],["\u003cp\u003eSecure your API keys by reviewing security best practices and using them as the preferred authentication method.\u003c/p\u003e\n"],["\u003cp\u003eImprove performance by utilizing techniques like exponential backoff for error handling, sending user-interaction requests on demand, and optimizing marker display.\u003c/p\u003e\n"],["\u003cp\u003eManage costs by setting budget alerts, capping daily API usage, and strategically using different Google Maps Platform products based on your needs.\u003c/p\u003e\n"],["\u003cp\u003eUnderstand Google Maps Platform quotas and how they apply to your API usage to avoid unexpected limitations.\u003c/p\u003e\n"]]],["Optimize Google Maps APIs usage by securing API keys, using them instead of client IDs, and implementing exponential backoff for errors. Enhance performance by sending requests on demand, avoiding overlays during map movement, using raster images for markers, and optimizing/clustering markers. Control consumption by setting budget alerts, capping daily usage, using static images when user interaction isn't needed, using the embed API for basic maps, and using mobile SDKs for mobile. In addition, you can reduce costs with optimized routing, limited waypoints, and cost-effective autocomplete and geocoding options.\n"],null,["# Optimization Guide\n\nThis guide describes several strategies to optimize your Google Maps APIs\nusage in terms of security, performance, and consumption.\n\nSecurity\n--------\n\n### Reviewing security best practices\n\nAPI keys are project-centric credentials that deserve the same precautions\nas user IDs and passwords. Review the\n[API Security Best Practices](/maps/api-security-best-practices) to secure your keys from\nunintended use which could lead to undue quota usage and unexpected charges\nto your account.\n\n### Using API keys to access Maps APIs\n\nAPI keys are the preferred authentication method for accessing\nGoogle Maps APIs. While using the client IDs is still\nsupported, API keys support finer-grained security controls and can be tuned to\nwork with specific web addresses, IP addresses, and mobile SDKs (Android and\niOS). For information on creating and securing an API key, go to the \"Using an\nAPI Key\" page for each API or SDK. (For example, for the\nMaps JavaScript API, visit its page on [Using an API\nKey](/maps/documentation/javascript/get-api-key).)\n\nPerformance\n-----------\n\n### Using exponential backoff to handle errors\n\nIf your apps experience errors from excessive attempts to call an API\nwithin a short period of time, such as quota errors, consider using\n[exponential backoff](http://en.wikipedia.org/wiki/Exponential_backoff) to let the requests process.\nExponential backoff is most useful for errors in the 500s.\n\nSpecifically, adjust the pace of your queries. In your code, add\na waiting period of **`S`** seconds between queries. If the query still results\nin a quota error, double the waiting period and then send another query. Continue\nadjusting the waiting period until the query returns without an error.\n\n### Sending user-interaction requests on demand\n\nRequests to APIs that include user interaction should be sent only on demand.\nThis means waiting for the end user to perform an action (such as `on-click`)\nto initiate the API request, then using the results to load a map, set a\ndestination, or display appropriate information. Using an on-demand approach\navoids unnecessary requests to the APIs, reducing API consumption.\n\n### Avoiding displaying overlay content when a map is moving\n\nAvoid using `Draw()` to display custom overlay content on a map at the same\ntime that a user might be moving the map. Since the map is redrawn every time\na user moves the map, placing overlay content on the map at the same time can\nintroduce lag or visual stuttering. Only add or remove overlay content from a\nmap once the user stops panning or zooming.\n\n### Avoiding intensive operations in `Draw` methods\n\nAs a general rule, it is good practice to avoid performance-intensive\nnon-drawing operations in a `Draw()` method. For example, avoid\nthe following in your `Draw()` method code:\n\n- Queries that return a large amount of content.\n- Many changes to the data being displayed.\n- Manipulating many Document Object Model (DOM) elements.\n\nThese operations can slow performance and introduce lag or visual stuttering\nwhen the map renders.\n\n### Using raster images for markers\n\nUse raster images, such as images in .PNG or .JPG format, when adding\nmarkers to identify a location on a map. Avoid using Scalable Vector\nGraphics (SVG) images, since rendering SVG images can introduce lag when\nthe map is redrawn.\n\n### Optimizing markers\n\nOptimization enhances performance by rendering many markers as a single static\nelement. This is useful in cases where a large number of markers is required.\nBy default, the Maps JavaScript API will decide whether a marker\nwill be optimized. When there is a large number of markers, the\nMaps JavaScript API will attempt to render markers with\noptimization. Not all Markers can be optimized; in some situations, the\nMaps JavaScript API may need to render Markers without\noptimization. Disable optimized rendering for animated GIFs or PNGs, or when\neach marker must be rendered as a separate DOM element.\n\n### Creating clusters to manage marker display\n\nTo help manage the display of markers to identify locations on a map,\ncreate a marker cluster using the\n[Marker Clusterer](https://github.com/googlemaps/js-marker-clusterer) library.\nThe Marker Clusterer library includes options for:\n\n- Grid size, to specify the number of markers to group together in a cluster.\n- Maximum zoom, to specify the maximum zoom level in which to display the cluster.\n- Image paths, for the graphics images to use as marker icons.\n\nConsumption\n-----------\n\nFor information on managing your Google Maps Platform costs, including\ncreating budgets, modifying quotas, and setting alerts, see\n[Manage costs](/maps/billing-and-pricing/manage-costs)."]]