با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این راهنما چندین استراتژی را برای بهینهسازی استفاده از Google Maps API از نظر امنیت، عملکرد و مصرف شرح میدهد.
امنیت
بررسی بهترین شیوه های امنیتی
کلیدهای API اعتبارنامههای پروژه محور هستند که مستحق اقدامات احتیاطی مشابه شناسههای کاربری و رمز عبور هستند. بهترین روشهای امنیتی API را مرور کنید تا کلیدهای خود را در برابر استفاده ناخواسته ایمن کنید که میتواند منجر به استفاده غیرمجاز از سهمیه و هزینههای غیرمنتظره از حساب شما شود.
استفاده از کلیدهای API برای دسترسی به Maps API
کلیدهای API روش احراز هویت ترجیحی برای دسترسی به APIهای Google Maps هستند. در حالی که استفاده از شناسه های مشتری همچنان پشتیبانی می شود، کلیدهای API از کنترل های امنیتی دقیق تری پشتیبانی می کنند و می توانند برای کار با آدرس های وب خاص، آدرس های IP و SDK های تلفن همراه (Android و iOS) تنظیم شوند. برای اطلاعات در مورد ایجاد و ایمن کردن یک کلید API، به صفحه «استفاده از کلید API» برای هر API یا SDK بروید. (به عنوان مثال، برای Maps JavaScript API، از صفحه آن در استفاده از کلید API دیدن کنید.)
عملکرد
استفاده از عقب نشینی نمایی برای رسیدگی به خطاها
اگر برنامههای شما به دلیل تلاشهای بیش از حد برای فراخوانی یک API در مدت زمان کوتاهی، خطاهایی مانند خطاهای سهمیهای را تجربه میکنند، برای اجازه دادن به پردازش درخواستها، از عقبنشینی نمایی استفاده کنید. عقب نشینی نمایی بیشتر برای خطاهای 500 مفید است.
به طور خاص، سرعت درخواست های خود را تنظیم کنید. در کد خود، یک دوره انتظار S ثانیه بین پرس و جوها اضافه کنید. اگر پرس و جو همچنان منجر به خطای سهمیه می شود، مدت زمان انتظار را دو برابر کنید و سپس درخواست دیگری ارسال کنید. تنظیم دوره انتظار را تا زمانی که پرس و جو بدون خطا برگردد ادامه دهید.
ارسال درخواست های تعامل کاربر در صورت تقاضا
درخواستهایی به APIهایی که شامل تعامل کاربر میشوند باید فقط در صورت درخواست ارسال شوند. این به این معنی است که منتظر بمانید تا کاربر نهایی یک عمل (مانند on-click ) را برای شروع درخواست API انجام دهد، سپس از نتایج برای بارگیری نقشه، تعیین مقصد یا نمایش اطلاعات مناسب استفاده کنید. استفاده از رویکرد درخواستی از درخواستهای غیرضروری به APIها جلوگیری میکند و مصرف API را کاهش میدهد.
اجتناب از نمایش محتوای همپوشانی زمانی که نقشه در حال حرکت است
از استفاده از Draw() برای نمایش محتوای همپوشانی سفارشی روی نقشه در همان زمانی که کاربر ممکن است نقشه را جابجا کند، خودداری کنید. از آنجایی که هر بار که کاربر نقشه را جابهجا میکند، نقشه دوباره ترسیم میشود، قرار دادن محتوای همزمان روی نقشه میتواند باعث تاخیر یا لکنت بصری شود. فقط زمانی محتوای همپوشانی را از نقشه اضافه یا حذف کنید که کاربر حرکت یا بزرگنمایی را متوقف کند.
اجتناب از عملیات فشرده در روش های Draw
به عنوان یک قاعده کلی، اجتناب از عملیات غیر ترسیمی فشرده در یک روش Draw() تمرین خوبی است. به عنوان مثال، در کد متد Draw() از موارد زیر اجتناب کنید:
کوئری هایی که حجم زیادی از محتوا را برمی گرداند.
تغییرات زیادی در داده های نمایش داده می شود.
دستکاری بسیاری از عناصر Document Object Model (DOM).
این عملیات می تواند عملکرد را کاهش دهد و در هنگام نمایش نقشه، تاخیر یا لکنت بصری را ایجاد کند.
استفاده از تصاویر شطرنجی برای نشانگرها
هنگام افزودن نشانگرها برای شناسایی مکان روی نقشه، از تصاویر شطرنجی، مانند تصاویر با فرمت PNG. یا JPG. استفاده کنید. از استفاده از تصاویر گرافیکی برداری مقیاس پذیر (SVG) خودداری کنید، زیرا رندر کردن تصاویر SVG می تواند در هنگام ترسیم مجدد نقشه، تاخیر ایجاد کند.
بهینه سازی نشانگرها
بهینه سازی عملکرد را با ارائه بسیاری از نشانگرها به عنوان یک عنصر ثابت افزایش می دهد. این در مواردی که به تعداد زیادی نشانگر نیاز است مفید است. به طور پیش فرض، Maps JavaScript API تصمیم می گیرد که آیا یک نشانگر بهینه شود یا خیر. هنگامی که تعداد زیادی نشانگر وجود دارد، Maps JavaScript API سعی می کند نشانگرها را با بهینه سازی ارائه کند. همه نشانگرها را نمی توان بهینه کرد. در برخی شرایط، Maps JavaScript API ممکن است نیاز به ارائه نشانگرها بدون بهینه سازی داشته باشد. غیرفعال کردن رندر بهینه برای GIF یا PNG متحرک، یا زمانی که هر نشانگر باید به عنوان یک عنصر DOM جداگانه ارائه شود.
ایجاد خوشه ها برای مدیریت نمایش نشانگر
برای کمک به مدیریت نمایش نشانگرها برای شناسایی مکان ها بر روی نقشه، با استفاده از کتابخانه نشانگر خوشه، یک خوشه نشانگر ایجاد کنید. کتابخانه Marker Clusterer شامل گزینه هایی برای موارد زیر است:
اندازه شبکه، برای تعیین تعداد نشانگرها برای گروه بندی در یک خوشه.
حداکثر زوم، برای تعیین حداکثر سطح بزرگنمایی که در آن خوشه نمایش داده شود.
مسیرهای تصویر، برای استفاده از تصاویر گرافیکی به عنوان نمادهای نشانگر.
مصرف
برای اطلاعات در مورد مدیریت هزینههای پلتفرم نقشههای Google، از جمله ایجاد بودجه، تغییر سهمیهها، و تنظیم هشدارها، به مدیریت هزینهها مراجعه کنید.
تاریخ آخرین بهروزرسانی 2025-08-09 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-09 بهوقت ساعت هماهنگ جهانی."],[[["\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)."]]