مقدمة
تحدّد العلامة موقعًا جغرافيًا على الخريطة. تستخدم العلامة تلقائيًا صورة عادية. يمكن أن تعرض العلامات صورًا مخصّصة، وفي هذه الحالة يُشار إليها عادةً باسم "الرموز".
العلامات والرموز هي كائنات من النوع
Marker. يمكنك ضبط رمز مخصّص ضمن أداة إنشاء العلامة، أو من خلال
استدعاء setIcon() على العلامة. مزيد من المعلومات عن
تخصيص صورة العلامة.
بشكل عام، العلامات هي نوع من التراكبات. للحصول على معلومات عن الأنواع الأخرى من التراكبات، يُرجى الاطّلاع على الرسم على الخريطة.
تم تصميم العلامات لتكون تفاعلية. على سبيل المثال، تتلقّى العلامات تلقائيًا أحداث 'click'، لذا يمكنك إضافة مستمع أحداث لعرض نافذة معلومات تعرض معلومات مخصّصة. يمكنك السماح للمستخدمين بنقل علامة على الـ
خريطة من خلال ضبط السمة draggable للعلامة على
true. لمزيد من المعلومات عن العلامات القابلة للسحب، يُرجى الاطّلاع
أدناه.
إضافة علامة
تأخذ أداة الإنشاء
google.maps.Marker
كائنًا حرفيًا واحدًا Marker options، يحدّد الخصائص الأولية للعلامة.
تتسم الحقول التالية بأهمية خاصة ويتم ضبطها عادةً عند إنشاء علامة:
-
position(مطلوبة): تحدّدLatLngتشير إلى الموقع الجغرافي الأولي للعلامة. إحدى طرق استردادLatLngهي استخدام خدمة الترميز الجغرافي. -
map(اختيارية): تحدّدMapالتي سيتم وضع العلامة عليها. إذا لم تحدّد الخريطة عند إنشاء العلامة، سيتم إنشاء العلامة ولكن لن يتم إرفاقها بالخريطة (أو عرضها عليها). يمكنك إضافة العلامة لاحقًا من خلال استدعاء طريقة العلامةsetMap()
يضيف المثال التالي علامة بسيطة إلى خريطة في أولورو، في الـ وسط أستراليا:
TypeScript
function initMap(): void { const myLatLng = { lat: -25.363, lng: 131.044 }; const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: myLatLng, } ); new google.maps.Marker({ position: myLatLng, map, title: "Hello World!", }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
function initMap() { const myLatLng = { lat: -25.363, lng: 131.044 }; const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: myLatLng, }); new google.maps.Marker({ position: myLatLng, map, title: "Hello World!", }); } window.initMap = initMap;
في المثال أعلاه، يتم وضع العلامة على الخريطة عند إنشاء
العلامة باستخدام السمة map في خيارات العلامة.
بدلاً من ذلك، يمكنك إضافة العلامة إلى الخريطة مباشرةً باستخدام طريقة
العلامة setMap()، كما هو موضّح في المثال أدناه:
var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapOptions = { zoom: 4, center: myLatlng } var map = new google.maps.Map(document.getElementById("map"), mapOptions); var marker = new google.maps.Marker({ position: myLatlng, title:"Hello World!" }); // To add the marker to the map, call setMap(); marker.setMap(map);
سيظهر title العلامة كتلميح.
إذا كنت لا تريد تمرير أي Marker options في أداة إنشاء العلامة، مرِّر بدلاً من ذلك كائنًا فارغًا {} في الوسيطة الأخيرة لأداة الإنشاء.
إزالة العلامة
لإزالة علامة من الخريطة، استدعِ طريقة setMap()
مع تمرير null كحجة.
marker.setMap(null);
يُرجى العِلم أنّ الطريقة أعلاه لا تحذف العلامة. بل تزيل العلامة من الخريطة. إذا كنت تريد بدلاً من ذلك حذف العلامة، عليك
إزالتها من الخريطة، ثم ضبط العلامة نفسها على
null.
إذا كنت تريد إدارة مجموعة من العلامات، عليك إنشاء مصفوفة لتضمين
العلامات. باستخدام هذه المصفوفة، يمكنك بعد ذلك استدعاء setMap() على
كل علامة في المصفوفة بدورها عندما تحتاج إلى إزالة العلامات. يمكنك حذف العلامات من خلال إزالتها من الخريطة ثم ضبط length المصفوفة على 0، ما يؤدي إلى إزالة جميع المراجع إلى العلامات.
تخصيص صورة العلامة
يمكنك تخصيص المظهر المرئي للعلامات من خلال تحديد ملف صورة أو رمز مستند إلى متجهات لعرضه بدلاً من رمز دبوس التثبيت التلقائي في "خرائط Google". يمكنك إضافة نص باستخدامتصنيف علامة، واستخدامرموز معقّدة لتحديد المناطق القابلة للنقر، وضبط ترتيب تكديس العلامات.
العلامات التي تتضمّن رموز صور
في الحالة الأساسية، يمكن أن يحدّد الرمز صورة لاستخدامها بدلاً من الـ
رمز دبوس التثبيت التلقائي في "خرائط Google". لتحديد رمز من هذا النوع، اضبط السمة
للعلامة icon على عنوان URL لصورة. سيغيّر Maps JavaScript API حجم الرمز تلقائيًا.
TypeScript
// This example adds a marker to indicate the position of Bondi Beach in Sydney, // Australia. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 4, center: { lat: -33, lng: 151 }, } ); const image = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png"; const beachMarker = new google.maps.Marker({ position: { lat: -33.89, lng: 151.274 }, map, icon: image, }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example adds a marker to indicate the position of Bondi Beach in Sydney, // Australia. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 4, center: { lat: -33, lng: 151 }, }); const image = "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png"; const beachMarker = new google.maps.Marker({ position: { lat: -33.89, lng: 151.274 }, map, icon: image, }); } window.initMap = initMap;
العلامات التي تتضمّن رموزًا مستندة إلى متجهات
يمكنك استخدام مسارات متجهات SVG مخصّصة لتحديد المظهر المرئي لـ
العلامات. لإجراء ذلك، مرِّر كائنًا حرفيًا Symbol يتضمّن المسار المطلوب إلى السمة icon للعلامة. يمكنك تحديد مسار مخصّص باستخدام تدوين مسار SVG، أو استخدام أحد المسارات المحدّدة مسبقًا في google.maps.SymbolPath. السمة anchor مطلوبة لكي يتم عرض العلامة بشكل صحيح عند تغيير مستوى التكبير/التصغير. مزيد من المعلومات عن
استخدام الرموز لإنشاء رموز مستندة إلى متجهات
للعلامات (والخطوط المتعددة)
TypeScript
// This example uses SVG path notation to add a vector-based symbol // as the icon for a marker. The resulting icon is a marker-shaped // symbol with a blue fill and no border. function initMap(): void { const center = new google.maps.LatLng(-33.712451, 150.311823); const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 9, center: center, } ); const svgMarker = { path: "M-1.547 12l6.563-6.609-1.406-1.406-5.156 5.203-2.063-2.109-1.406 1.406zM0 0q2.906 0 4.945 2.039t2.039 4.945q0 1.453-0.727 3.328t-1.758 3.516-2.039 3.070-1.711 2.273l-0.75 0.797q-0.281-0.328-0.75-0.867t-1.688-2.156-2.133-3.141-1.664-3.445-0.75-3.375q0-2.906 2.039-4.945t4.945-2.039z", fillColor: "blue", fillOpacity: 0.6, strokeWeight: 0, rotation: 0, scale: 2, anchor: new google.maps.Point(0, 20), }; new google.maps.Marker({ position: map.getCenter(), icon: svgMarker, map: map, }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// This example uses SVG path notation to add a vector-based symbol // as the icon for a marker. The resulting icon is a marker-shaped // symbol with a blue fill and no border. function initMap() { const center = new google.maps.LatLng(-33.712451, 150.311823); const map = new google.maps.Map(document.getElementById("map"), { zoom: 9, center: center, }); const svgMarker = { path: "M-1.547 12l6.563-6.609-1.406-1.406-5.156 5.203-2.063-2.109-1.406 1.406zM0 0q2.906 0 4.945 2.039t2.039 4.945q0 1.453-0.727 3.328t-1.758 3.516-2.039 3.070-1.711 2.273l-0.75 0.797q-0.281-0.328-0.75-0.867t-1.688-2.156-2.133-3.141-1.664-3.445-0.75-3.375q0-2.906 2.039-4.945t4.945-2.039z", fillColor: "blue", fillOpacity: 0.6, strokeWeight: 0, rotation: 0, scale: 2, anchor: new google.maps.Point(0, 20), }; new google.maps.Marker({ position: map.getCenter(), icon: svgMarker, map: map, }); } window.initMap = initMap;
تصنيفات العلامات
تصنيف العلامة هو حرف أو رقم يظهر داخل العلامة. تعرض صورة العلامة في هذا القسم تصنيفًا يتضمّن الحرف "B". يمكنك تحديد تصنيف العلامة كإما سلسلة أو كائن
MarkerLabel
يتضمّن سلسلة وخصائص تصنيف أخرى.
عند إنشاء علامة، يمكنك تحديد السمة label في
الكائن
MarkerOptions. بدلاً من ذلك، يمكنك استدعاء setLabel() على الكائن
Marker
لضبط التصنيف على علامة حالية.
يعرض المثال التالي علامات مصنّفة عندما ينقر المستخدم على الـ خريطة:
TypeScript
// In the following example, markers appear when the user clicks on the map. // Each marker is labeled with a single alphabetical character. const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let labelIndex = 0; function initMap(): void { const bangalore = { lat: 12.97, lng: 77.59 }; const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 12, center: bangalore, } ); // This event listener calls addMarker() when the map is clicked. google.maps.event.addListener(map, "click", (event) => { addMarker(event.latLng, map); }); // Add a marker at the center of the map. addMarker(bangalore, map); } // Adds a marker to the map. function addMarker(location: google.maps.LatLngLiteral, map: google.maps.Map) { // Add the marker at the clicked location, and add the next-available label // from the array of alphabetical characters. new google.maps.Marker({ position: location, label: labels[labelIndex++ % labels.length], map: map, }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// In the following example, markers appear when the user clicks on the map. // Each marker is labeled with a single alphabetical character. const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; let labelIndex = 0; function initMap() { const bangalore = { lat: 12.97, lng: 77.59 }; const map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: bangalore, }); // This event listener calls addMarker() when the map is clicked. google.maps.event.addListener(map, "click", (event) => { addMarker(event.latLng, map); }); // Add a marker at the center of the map. addMarker(bangalore, map); } // Adds a marker to the map. function addMarker(location, map) { // Add the marker at the clicked location, and add the next-available label // from the array of alphabetical characters. new google.maps.Marker({ position: location, label: labels[labelIndex++ % labels.length], map: map, }); } window.initMap = initMap;
الرموز المعقّدة
يمكنك تحديد أشكال معقّدة للإشارة إلى المناطق القابلة للنقر، وتحديد كيفية ظهور الرموز بالنسبة إلى التراكبات الأخرى (ترتيب تكديسها). يجب أن تضبط الرموز المحدّدة بهذه الطريقة سماتها
icon
على كائن من النوع
Icon.
Icon
تحدّد كائنات صورة. تحدّد أيضًا size الرمز وorigin الرمز (إذا كانت الصورة التي تريدها جزءًا من صورة أكبر في صورة مجمّعة، مثلاً) وanchor حيث يجب وضع النقطة الفعّالة للرمز (التي تستند إلى المصدر).
إذا كنت تستخدم تصنيفًا مع علامة مخصّصة، يمكنك وضع التصنيف باستخدام السمة labelOrigin في الكائن Icon.
TypeScript
// The following example creates complex markers to indicate beaches near // Sydney, NSW, Australia. Note that the anchor is set to (0,32) to correspond // to the base of the flagpole. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 10, center: { lat: -33.9, lng: 151.2 }, } ); setMarkers(map); } // Data for the markers consisting of a name, a LatLng and a zIndex for the // order in which these markers should display on top of each other. const beaches: [string, number, number, number][] = [ ["Bondi Beach", -33.890542, 151.274856, 4], ["Coogee Beach", -33.923036, 151.259052, 5], ["Cronulla Beach", -34.028249, 151.157507, 3], ["Manly Beach", -33.80010128657071, 151.28747820854187, 2], ["Maroubra Beach", -33.950198, 151.259302, 1], ]; function setMarkers(map: google.maps.Map) { // Adds markers to the map. // Marker sizes are expressed as a Size of X,Y where the origin of the image // (0,0) is located in the top left of the image. // Origins, anchor positions and coordinates of the marker increase in the X // direction to the right and in the Y direction down. const image = { url: "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png", // This marker is 20 pixels wide by 32 pixels high. size: new google.maps.Size(20, 32), // The origin for this image is (0, 0). origin: new google.maps.Point(0, 0), // The anchor for this image is the base of the flagpole at (0, 32). anchor: new google.maps.Point(0, 32), }; // Shapes define the clickable region of the icon. The type defines an HTML // <area> element 'poly' which traces out a polygon as a series of X,Y points. // The final coordinate closes the poly by connecting to the first coordinate. const shape = { coords: [1, 1, 1, 20, 18, 20, 18, 1], type: "poly", }; for (let i = 0; i < beaches.length; i++) { const beach = beaches[i]; new google.maps.Marker({ position: { lat: beach[1], lng: beach[2] }, map, icon: image, shape: shape, title: beach[0], zIndex: beach[3], }); } } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// The following example creates complex markers to indicate beaches near // Sydney, NSW, Australia. Note that the anchor is set to (0,32) to correspond // to the base of the flagpole. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 10, center: { lat: -33.9, lng: 151.2 }, }); setMarkers(map); } // Data for the markers consisting of a name, a LatLng and a zIndex for the // order in which these markers should display on top of each other. const beaches = [ ["Bondi Beach", -33.890542, 151.274856, 4], ["Coogee Beach", -33.923036, 151.259052, 5], ["Cronulla Beach", -34.028249, 151.157507, 3], ["Manly Beach", -33.80010128657071, 151.28747820854187, 2], ["Maroubra Beach", -33.950198, 151.259302, 1], ]; function setMarkers(map) { // Adds markers to the map. // Marker sizes are expressed as a Size of X,Y where the origin of the image // (0,0) is located in the top left of the image. // Origins, anchor positions and coordinates of the marker increase in the X // direction to the right and in the Y direction down. const image = { url: "https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png", // This marker is 20 pixels wide by 32 pixels high. size: new google.maps.Size(20, 32), // The origin for this image is (0, 0). origin: new google.maps.Point(0, 0), // The anchor for this image is the base of the flagpole at (0, 32). anchor: new google.maps.Point(0, 32), }; // Shapes define the clickable region of the icon. The type defines an HTML // <area> element 'poly' which traces out a polygon as a series of X,Y points. // The final coordinate closes the poly by connecting to the first coordinate. const shape = { coords: [1, 1, 1, 20, 18, 20, 18, 1], type: "poly", }; for (let i = 0; i < beaches.length; i++) { const beach = beaches[i]; new google.maps.Marker({ position: { lat: beach[1], lng: beach[2] }, map, icon: image, shape: shape, title: beach[0], zIndex: beach[3], }); } } window.initMap = initMap;
تحويل كائنات MarkerImage إلى النوع Icon
حتى الإصدار 3.10 من Maps JavaScript API، كانت الرموز المعقّدة
محدّدة ككائنات MarkerImage. تمت إضافة الكائن الحرفي
Icon في الإصدار 3.10، ويحلّ محلّ
MarkerImage بدءًا من الإصدار 3.11.
تتوافق الكائنات الحرفية Icon مع المَعلمات نفسها التي تتوافق مع MarkerImage، ما يسمح لك بتحويل MarkerImage بسهولة إلى Icon من خلال إزالة أداة الإنشاء، وتضمين المَعلمات السابقة في {}، وإضافة أسماء كل مَعلمة. على سبيل المثال:
var image = new google.maps.MarkerImage( place.icon, new google.maps.Size(71, 71), new google.maps.Point(0, 0), new google.maps.Point(17, 34), new google.maps.Size(25, 25));
يصبح
var image = { url: place.icon, size: new google.maps.Size(71, 71), origin: new google.maps.Point(0, 0), anchor: new google.maps.Point(17, 34), scaledSize: new google.maps.Size(25, 25) };
تحسين العلامات
يحسّن التحسين الأداء من خلال عرض العديد من العلامات كعنصر ثابت واحد. ويكون ذلك مفيدًا في الحالات التي تتطلب عددًا كبيرًا من العلامات. سيقرّر Maps JavaScript API تلقائيًا ما إذا سيتم تحسين العلامة. عندما يكون هناك عدد كبير من العلامات، سيحاول Maps JavaScript API عرض العلامات مع التحسين. لا يمكن تحسين جميع العلامات. في بعض الحالات، قد يحتاج Maps JavaScript API إلى عرض العلامات بدون تحسين. يمكنك إيقاف العرض المحسّن لصور GIF أو PNG المتحرّكة، أو عندما يجب عرض كل علامة كعنصر DOM منفصل. يعرض المثال التالي كيفية إنشاء علامة محسّنة:
var marker = new google.maps.Marker({ position: myLatlng, title:"Hello World!", optimized: true });
تسهيل الوصول إلى العلامة
يمكنك تسهيل الوصول إلى العلامة من خلال إضافة حدث متتبِّع النقرات، و
ضبط optimized على false. يؤدي متتبِّع النقرات إلى منح العلامة معاني الزر، التي يمكن الوصول إليها باستخدام التنقّل عبر لوحة المفاتيح وقارئات الشاشة وما إلى ذلك. استخدِم الخيار
title لعرض نص يسهل الوصول إليه للعلامة.
في المثال التالي، تتلقّى العلامة الأولى التركيز عند الضغط على المفتاح Tab . يمكنك بعد ذلك استخدام مفاتيح الأسهم للتنقّل بين العلامات. اضغط على المفتاح Tab مرة أخرى لمواصلة التنقّل بين عناصر التحكّم الأخرى في الخريطة. إذا كانت العلامة تتضمّن نافذة معلومات، يمكنك فتحها من خلال النقر على العلامة، أو من خلال الضغط على مفتاح Enter أو مفتاح المسافة عندما تكون العلامة محدّدة. عند إغلاق نافذة المعلومات، سيعود التركيز إلى العلامة المرتبطة.
TypeScript
// The following example creates five accessible and // focusable markers. function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 12, center: { lat: 34.84555, lng: -111.8035 }, } ); // Set LatLng and title text for the markers. The first marker (Boynton Pass) // receives the initial focus when tab is pressed. Use arrow keys to // move between markers; press tab again to cycle through the map controls. const tourStops: [google.maps.LatLngLiteral, string][] = [ [{ lat: 34.8791806, lng: -111.8265049 }, "Boynton Pass"], [{ lat: 34.8559195, lng: -111.7988186 }, "Airport Mesa"], [{ lat: 34.832149, lng: -111.7695277 }, "Chapel of the Holy Cross"], [{ lat: 34.823736, lng: -111.8001857 }, "Red Rock Crossing"], [{ lat: 34.800326, lng: -111.7665047 }, "Bell Rock"], ]; // Create an info window to share between markers. const infoWindow = new google.maps.InfoWindow(); // Create the markers. tourStops.forEach(([position, title], i) => { const marker = new google.maps.Marker({ position, map, title: `${i + 1}. ${title}`, label: `${i + 1}`, optimized: false, }); // Add a click listener for each marker, and set up the info window. marker.addListener("click", () => { infoWindow.close(); infoWindow.setContent(marker.getTitle()); infoWindow.open(marker.getMap(), marker); }); }); } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// The following example creates five accessible and // focusable markers. function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: { lat: 34.84555, lng: -111.8035 }, }); // Set LatLng and title text for the markers. The first marker (Boynton Pass) // receives the initial focus when tab is pressed. Use arrow keys to // move between markers; press tab again to cycle through the map controls. const tourStops = [ [{ lat: 34.8791806, lng: -111.8265049 }, "Boynton Pass"], [{ lat: 34.8559195, lng: -111.7988186 }, "Airport Mesa"], [{ lat: 34.832149, lng: -111.7695277 }, "Chapel of the Holy Cross"], [{ lat: 34.823736, lng: -111.8001857 }, "Red Rock Crossing"], [{ lat: 34.800326, lng: -111.7665047 }, "Bell Rock"], ]; // Create an info window to share between markers. const infoWindow = new google.maps.InfoWindow(); // Create the markers. tourStops.forEach(([position, title], i) => { const marker = new google.maps.Marker({ position, map, title: `${i + 1}. ${title}`, label: `${i + 1}`, optimized: false, }); // Add a click listener for each marker, and set up the info window. marker.addListener("click", () => { infoWindow.close(); infoWindow.setContent(marker.getTitle()); infoWindow.open(marker.getMap(), marker); }); }); } window.initMap = initMap;
تحريك العلامة
يمكنك تحريك العلامات بحيث تعرض حركة ديناميكية في مجموعة متنوّعة
من الظروف المختلفة. لتحديد طريقة تحريك العلامة، استخدِم
السمة animation للعلامة، من النوع
google.maps.Animation. القيم التالية
Animation متوافقة:
-
DROPيشير إلى أنّ العلامة يجب أن تنزل من أعلى الخريطة إلى موقعها الجغرافي النهائي عند وضعها لأول مرة على الخريطة. ستتوقف الحركة بمجرد أن تستقر العلامة وسيعودanimationإلىnull. عادةً ما يتم تحديد هذا النوع من الحركة أثناء الإنشاءMarker. -
BOUNCE: يشير إلى أنّ العلامة يجب أن ترتد في مكانها. ستستمر العلامة في الارتداد إلى أن يتم ضبط السمةanimationبشكل صريح علىnull.
يمكنك بدء حركة على علامة حالية من خلال استدعاء
setAnimation() على الكائن Marker
TypeScript
// The following example creates a marker in Stockholm, Sweden using a DROP // animation. Clicking on the marker will toggle the animation between a BOUNCE // animation and no animation. let marker: google.maps.Marker; function initMap(): void { const map = new google.maps.Map( document.getElementById("map") as HTMLElement, { zoom: 13, center: { lat: 59.325, lng: 18.07 }, } ); marker = new google.maps.Marker({ map, draggable: true, animation: google.maps.Animation.DROP, position: { lat: 59.327, lng: 18.067 }, }); marker.addListener("click", toggleBounce); } function toggleBounce() { if (marker.getAnimation() !== null) { marker.setAnimation(null); } else { marker.setAnimation(google.maps.Animation.BOUNCE); } } declare global { interface Window { initMap: () => void; } } window.initMap = initMap;
JavaScript
// The following example creates a marker in Stockholm, Sweden using a DROP // animation. Clicking on the marker will toggle the animation between a BOUNCE // animation and no animation. let marker; function initMap() { const map = new google.maps.Map(document.getElementById("map"), { zoom: 13, center: { lat: 59.325, lng: 18.07 }, }); marker = new google.maps.Marker({ map, draggable: true, animation: google.maps.Animation.DROP, position: { lat: 59.327, lng: 18.067 }, }); marker.addListener("click", toggleBounce); } function toggleBounce() { if (marker.getAnimation() !== null) { marker.setAnimation(null); } else { marker.setAnimation(google.maps.Animation.BOUNCE); } } window.initMap = initMap;
إذا كان لديك العديد من العلامات، قد لا تريد إنزالها على الخريطة كلها مرة واحدة. يمكنك استخدام setTimeout() لضبط مسافات بين حركات العلامات باستخدام نمط مشابه للنمط الموضّح أدناه:
function drop() { for (var i =0; i < markerArray.length; i++) { setTimeout(function() { addMarkerMethod(); }, i * 200); } }
جعل العلامة قابلة للسحب
للسماح للمستخدمين بسحب علامة إلى موقع جغرافي مختلف على الخريطة، اضبط
draggable على true في خيارات العلامة.
var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapOptions = { zoom: 4, center: myLatlng } var map = new google.maps.Map(document.getElementById("map"), mapOptions); // Place a draggable marker on the map var marker = new google.maps.Marker({ position: myLatlng, map: map, draggable:true, title:"Drag me!" });
مزيد من خيارات تخصيص العلامة
للحصول على مزيد من الإضافات إلى فئة العلامة، وتجميع العلامات وإدارتها، وتخصيص التراكبات، يُرجى الاطّلاع على المكتبات المفتوحة المصدر.