ตัวทำเครื่องหมาย

เลือกแพลตฟอร์ม: Android iOS JavaScript

ข้อมูลเบื้องต้น

เครื่องหมายระบุตําแหน่งบนแผนที่ ตัวทําเครื่องหมายจะใช้รูปภาพมาตรฐานโดยค่าเริ่มต้น เครื่องหมายสามารถแสดงรูปภาพที่กําหนดเอง ซึ่งในกรณีดังกล่าวโดยทั่วไปจะเรียกว่า "ไอคอน" เครื่องหมายและไอคอนเป็นวัตถุประเภท Marker คุณตั้งค่าไอคอนที่กําหนดเองภายในตัวสร้างเครื่องหมาย หรือโดยการโทรหา setIcon() ในเครื่องหมายได้ ดูข้อมูลเพิ่มเติมเกี่ยวกับการปรับแต่งรูปภาพเครื่องหมาย

เครื่องหมายที่ใช้ทั่วไปเป็นการวางซ้อนประเภทหนึ่ง สําหรับข้อมูลเพิ่มเติม เกี่ยวกับการวางซ้อนประเภทอื่นๆ โปรดดู การวาดบนแผนที่

เครื่องหมายได้รับการออกแบบมาให้เป็นแบบอินเทอร์แอ็กทีฟ ตัวอย่างเช่น โดยค่าเริ่มต้น ผู้ใช้จะได้รับเหตุการณ์ 'click' คุณจึงเพิ่ม Listener เหตุการณ์เพื่อเปิดหน้าต่างข้อมูลที่แสดงข้อมูลที่กําหนดเองได้ คุณอนุญาตให้ผู้ใช้ย้ายเครื่องหมายบนแผนที่ได้โดยการตั้งค่าพร็อพเพอร์ตี้ draggable ของเครื่องหมายเป็น true ดูข้อมูลเพิ่มเติมเกี่ยวกับเครื่องหมายที่ลากได้ด้านล่าง

เพิ่มเครื่องหมาย

เครื่องมือสร้าง google.maps.Marker จะใช้ออบเจ็กต์ Marker options แบบลิเทอรัลเพียง 1 รายการ โดยระบุพร็อพเพอร์ตี้เริ่มต้นของเครื่องหมาย

ช่องต่อไปนี้มีความสําคัญเป็นพิเศษและมักจะตั้งค่าเมื่อสร้างเครื่องหมาย:

  • position (ต้องระบุ) ระบุ LatLng ซึ่งระบุตําแหน่งเริ่มต้นของเครื่องหมาย วิธีหนึ่งในการเรียก LatLng คือการใช้บริการการระบุพิกัดทางภูมิศาสตร์
  • map (ไม่บังคับ) ระบุ Map ที่จะวางเครื่องหมาย หากคุณไม่ได้ระบุแผนที่ในขั้นตอนการสร้าง เครื่องหมายดังกล่าวจะถูกสร้างแต่ไม่ได้แนบ (หรือแสดงอยู่บน) แผนที่ คุณอาจเพิ่มเครื่องหมายในภายหลังโดยเรียกเมธอด setMap() ของเครื่องหมาย

ตัวอย่างต่อไปนี้เพิ่มเครื่องหมายแบบง่ายลงในแผนที่ที่ Uluru ใจกลางออสเตรเลีย:

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);

The marker's title will appear as a tooltip.

If you do not wish to pass any Marker options in the marker's constructor, instead pass an empty object {} in the last argument of the constructor.

View example

Remove a marker

To remove a marker from the map, call the setMap() method passing null as the argument.

marker.setMap(null);

Note that the above method does not delete the marker. It removes the marker from the map. If instead you wish to delete the marker, you should remove it from the map, and then set the marker itself to null.

If you wish to manage a set of markers, you should create an array to hold the markers. Using this array, you can then call setMap() on each marker in the array in turn when you need to remove the markers. You can delete the markers by removing them from the map and then setting the array's length to 0, which removes all references to the markers.

View example

Customize a marker image

You can customize the visual appearance of markers by specifying an image file or vector-based icon to display instead of the default Google Maps pushpin icon. You can add text with a marker label, and use complex icons to define clickable regions, and set the stack order of markers.

Markers with image icons

In the most basic case, an icon can specify an image to use instead of the default Google Maps pushpin icon. To specify such an icon, set the marker's icon property to the URL of an image. The Maps JavaScript API will size the icon automatically.

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

Maps JavaScript API ระบุว่าไอคอนที่ซับซ้อนเป็นออบเจ็กต์ MarkerImage จนถึงเวอร์ชัน 3.10 ของ Maps JavaScript API มีการเพิ่มออบเจ็กต์ออบเจ็กต์ 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));

becomes

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)
};

Optimize markers

Optimization enhances performance by rendering many markers as a single static element. This is useful in cases where a large number of markers is required. By default, the Maps JavaScript API will decide whether a marker will be optimized. When there is a large number of markers, the Maps JavaScript API will attempt to render markers with optimization. Not all Markers can be optimized; in some situations, the Maps JavaScript API may need to render Markers without optimization. Disable optimized rendering for animated GIFs or PNGs, or when each marker must be rendered as a separate DOM element. The following example shows creating an optimized marker:

var marker = new google.maps.Marker({
    position: myLatlng,
    title:"Hello World!",
    optimized: true 
});

กําหนดให้เครื่องหมายเข้าถึงได้

คุณทําให้ตัวทําเครื่องหมายเข้าถึงได้โดยการเพิ่มเหตุการณ์ Listener การคลิก และตั้งค่า optimized เป็น false Listener การคลิก จะทําให้เครื่องหมายมีความหมายของปุ่ม ซึ่งสามารถเข้าถึงได้โดยใช้ การนําทางด้วยแป้นพิมพ์ โปรแกรมอ่านหน้าจอ และอื่นๆ ใช้ตัวเลือก title เพื่อแสดงข้อความที่เข้าถึงได้สําหรับเครื่องหมาย

ในตัวอย่างต่อไปนี้ เครื่องหมายแรกจะโฟกัสเมื่อกดแท็บ จากนั้นคุณจะใช้แป้นลูกศรเพื่อเลื่อนไปยังเครื่องหมายต่างๆ ได้ กด 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!"
});

การปรับแต่งเครื่องหมายเพิ่มเติม

สําหรับเครื่องหมายที่ออกแบบเองทั้งหมด โปรดดูตัวอย่างป๊อปอัปที่ปรับแต่งแล้ว

สําหรับส่วนขยายเพิ่มเติมของมาร์กเกอร์คลาส คลัสเตอร์และการจัดการเครื่องหมาย ตลอดจนการปรับแต่งการวางซ้อน โปรดดูที่ไลบรารีโอเพนซอร์ส