بررسی اجمالی
این آموزش به شما یاد می دهد که چگونه نماد یک نشانگر نقشه های گوگل را تغییر دهید. دانستن اصول اولیه ایجاد نشانگر هنگام استفاده از این آموزش مفید است.
نقشه زیر نمونه ای از نقشه هایی است که از نشانگرهای سفارشی شده استفاده می کند.
بخش زیر تمام کدهایی را که برای ایجاد نقشه در این آموزش نیاز دارید فهرست می کند.
TypeScript
let map: google.maps.Map; function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { center: new google.maps.LatLng(-33.91722, 151.23064), zoom: 16, }); const iconBase = "https://developers.google.com/maps/documentation/javascript/examples/full/images/"; const icons: Record<string, { icon: string }> = { parking: { icon: iconBase + "parking_lot_maps.png", }, library: { icon: iconBase + "library_maps.png", }, info: { icon: iconBase + "info-i_maps.png", }, }; const features = [ { position: new google.maps.LatLng(-33.91721, 151.2263), type: "info", }, { position: new google.maps.LatLng(-33.91539, 151.2282), type: "info", }, { position: new google.maps.LatLng(-33.91747, 151.22912), type: "info", }, { position: new google.maps.LatLng(-33.9191, 151.22907), type: "info", }, { position: new google.maps.LatLng(-33.91725, 151.23011), type: "info", }, { position: new google.maps.LatLng(-33.91872, 151.23089), type: "info", }, { position: new google.maps.LatLng(-33.91784, 151.23094), type: "info", }, { position: new google.maps.LatLng(-33.91682, 151.23149), type: "info", }, { position: new google.maps.LatLng(-33.9179, 151.23463), type: "info", }, { position: new google.maps.LatLng(-33.91666, 151.23468), type: "info", }, { position: new google.maps.LatLng(-33.916988, 151.23364), type: "info", }, { position: new google.maps.LatLng(-33.91662347903106, 151.22879464019775), type: "parking", }, { position: new google.maps.LatLng(-33.916365282092855, 151.22937399734496), type: "parking", }, { position: new google.maps.LatLng(-33.91665018901448, 151.2282474695587), type: "parking", }, { position: new google.maps.LatLng(-33.919543720969806, 151.23112279762267), type: "parking", }, { position: new google.maps.LatLng(-33.91608037421864, 151.23288232673644), type: "parking", }, { position: new google.maps.LatLng(-33.91851096391805, 151.2344058214569), type: "parking", }, { position: new google.maps.LatLng(-33.91818154739766, 151.2346203981781), type: "parking", }, { position: new google.maps.LatLng(-33.91727341958453, 151.23348314155578), type: "library", }, ]; // Create markers. for (let i = 0; i < features.length; i++) { const marker = new google.maps.Marker({ position: features[i].position, icon: icons[features[i].type].icon, map: map, }); } } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
جاوا اسکریپت
let map; function initMap() { map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(-33.91722, 151.23064), zoom: 16, }); const iconBase = "https://developers.google.com/maps/documentation/javascript/examples/full/images/"; const icons = { parking: { icon: iconBase + "parking_lot_maps.png", }, library: { icon: iconBase + "library_maps.png", }, info: { icon: iconBase + "info-i_maps.png", }, }; const features = [ { position: new google.maps.LatLng(-33.91721, 151.2263), type: "info", }, { position: new google.maps.LatLng(-33.91539, 151.2282), type: "info", }, { position: new google.maps.LatLng(-33.91747, 151.22912), type: "info", }, { position: new google.maps.LatLng(-33.9191, 151.22907), type: "info", }, { position: new google.maps.LatLng(-33.91725, 151.23011), type: "info", }, { position: new google.maps.LatLng(-33.91872, 151.23089), type: "info", }, { position: new google.maps.LatLng(-33.91784, 151.23094), type: "info", }, { position: new google.maps.LatLng(-33.91682, 151.23149), type: "info", }, { position: new google.maps.LatLng(-33.9179, 151.23463), type: "info", }, { position: new google.maps.LatLng(-33.91666, 151.23468), type: "info", }, { position: new google.maps.LatLng(-33.916988, 151.23364), type: "info", }, { position: new google.maps.LatLng(-33.91662347903106, 151.22879464019775), type: "parking", }, { position: new google.maps.LatLng(-33.916365282092855, 151.22937399734496), type: "parking", }, { position: new google.maps.LatLng(-33.91665018901448, 151.2282474695587), type: "parking", }, { position: new google.maps.LatLng(-33.919543720969806, 151.23112279762267), type: "parking", }, { position: new google.maps.LatLng(-33.91608037421864, 151.23288232673644), type: "parking", }, { position: new google.maps.LatLng(-33.91851096391805, 151.2344058214569), type: "parking", }, { position: new google.maps.LatLng(-33.91818154739766, 151.2346203981781), type: "parking", }, { position: new google.maps.LatLng(-33.91727341958453, 151.23348314155578), type: "library", }, ]; // Create markers. for (let i = 0; i < features.length; i++) { const marker = new google.maps.Marker({ position: features[i].position, icon: icons[features[i].type].icon, map: map, }); } } window.initMap = initMap;
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html> <head> <title>Custom Markers</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div id="map"></div> <!-- The `defer` attribute causes the callback to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly" defer ></script> </body> </html>
Sample را امتحان کنید
سفارشی کردن نشانگر نقشه
تصویر زیر نشانگر نقشه گوگل را با نماد قرمز پیش فرض نشان می دهد.
شما می توانید این نماد را به تصویر دلخواه خود تغییر دهید. جدول زیر کدی را توضیح می دهد که نشانگر پیش فرض را برای استفاده از نماد برای پارکینگ ها سفارشی می کند.
کد و توضیحات | |
---|---|
| ویژگی icon را به شی MarkerOptions اضافه می کند تا نماد نشانگر را تغییر دهد.ویژگی icon می تواند یک رشته (URL به نماد نشانگر) یا یک شی Icon باشد.نماد نشانگر سفارشی شده را در زیر ببینید. ![]() |
سفارشی کردن نشانگرها بر اساس ویژگی های نقشه
هر نقطه مورد علاقه در لیست ویژگی های دانشگاه دارای یک ویژگی type
است. توجه داشته باشید که چگونه استخراج کد زیر انواع parking
، library
و info
را مشخص می کند. میتوانید نماد نشانگر را بسته به type
ویژگی نقشه که روی آن تنظیم کردهاید، سفارشی کنید.
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
}
اطلاعات بیشتر
- تصویر روی یک نشانگر "نماد" آن نامیده می شود.
- نشانگر به عنوان "پین" نیز شناخته می شود.