ย้ายข้อมูลไปยังเครื่องหมายขั้นสูง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ในวันที่ 21 กุมภาพันธ์ 2024 (v3.56) เราจะเลิกใช้งาน google.maps.Marker เราขอแนะนำให้คุณเปลี่ยนไปใช้google.maps.marker.AdvancedMarkerElement
ชั้นเรียนใหม่ เครื่องหมายขั้นสูงช่วยปรับปรุงได้อย่างมากเมื่อเทียบกับคลาส google.maps.Marker
รุ่นเดิม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเลิกใช้งานนี้
หากต้องการอัปเดตเครื่องหมายเดิมให้เป็นเครื่องหมายขั้นสูง ให้ทำตามขั้นตอนต่อไปนี้
- เพิ่มโค้ดเพื่อนำเข้าไลบรารีเครื่องหมาย โปรดทราบว่าเครื่องหมายเวอร์ชันก่อนหน้า (
google.maps.Marker
) ไม่ได้มีข้อกำหนดนี้
- เปลี่ยน
google.maps.Marker
เป็น google.maps.marker.AdvancedMarkerElement
- เพิ่มรหัสแผนที่ลงในโค้ดการเริ่มต้นแผนที่ เช่น
mapId: 'DEMO_MAP_ID'
เพื่อวัตถุประสงค์ในการทดสอบหากคุณยังไม่มีรหัสแผนที่
เพิ่มไลบรารีเครื่องหมายขั้นสูง
วิธีการโหลดไลบรารีจะขึ้นอยู่กับวิธีที่หน้าเว็บโหลด Maps JavaScript API
หากหน้าเว็บใช้การโหลดสคริปต์แบบไดนามิก ให้เพิ่มไลบรารีเครื่องหมายและ
นำเข้า AdvancedMarkerElement
(และ PinElement
หากต้องการ) ในรันไทม์ตามที่
แสดงที่นี่
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker");
หากหน้าเว็บใช้แท็กการโหลดสคริปต์โดยตรงแบบเดิม ให้เพิ่ม
libraries=marker
ลงในสคริปต์การโหลดตามที่แสดงในข้อมูลโค้ดต่อไปนี้
<script
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&libraries=marker"
defer
></script>
ดูข้อมูลเพิ่มเติมเกี่ยวกับการโหลด Maps JavaScript API
ตัวอย่าง
ตัวอย่างโค้ดต่อไปนี้แสดงโค้ดสําหรับการเพิ่มเครื่องหมายเดิม ตามด้วย
โค้ดสําหรับตัวอย่างเดียวกันโดยใช้เครื่องหมายขั้นสูง
ก่อนการย้ายข้อมูล
// The location of Uluru
const position = { lat: -25.344, lng: 131.031 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: position,
});
// The marker, positioned at Uluru
const marker = new google.maps.Marker({
map: map,
position: position,
title: 'Uluru',
});
หลังการย้ายข้อมูล
// The location of Uluru
const position = { lat: -25.344, lng: 131.031 };
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: position,
mapId: "DEMO_MAP_ID", // Map ID is required for advanced markers.
});
// The advanced marker, positioned at Uluru
const marker = new google.maps.marker.AdvancedMarkerElement({
map,
position: position,
title: 'Uluru',
});
สำรวจฟีเจอร์เครื่องหมายขั้นสูง
คุณปรับแต่งเครื่องหมายขั้นสูงได้ในแบบที่ก่อนหน้านี้ทำไม่ได้
ตอนนี้คุณสามารถปรับขนาด (สเกล) ของเครื่องหมาย รวมถึงเปลี่ยนสีของ
พื้นหลัง เส้นขอบ และสัญลักษณ์ได้แล้ว รูปภาพกราฟิกที่กำหนดเองใช้งานได้ง่ายขึ้น
และตอนนี้คุณสามารถเขียนเครื่องหมายที่กำหนดเองโดยใช้ HTML และ CSS ได้แล้ว ดูข้อมูลเพิ่มเติม
เกี่ยวกับทุกสิ่งที่คุณทำได้ด้วยเครื่องหมายขั้นสูง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-17 UTC
[null,null,["อัปเดตล่าสุด 2025-08-17 UTC"],[[["\u003cp\u003e\u003ccode\u003egoogle.maps.Marker\u003c/code\u003e is deprecated as of February 21st, 2024 (v3.56) and developers should transition to \u003ccode\u003egoogle.maps.marker.AdvancedMarkerElement\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAdvanced markers require adding the marker library, changing the class name, and including a map ID during map initialization.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can customize advanced markers with features like adjustable size, custom colors, graphic images, and HTML/CSS composition.\u003c/p\u003e\n"],["\u003cp\u003eSeveral resources are available to learn more about advanced marker features such as basic customization, graphic markers, HTML/CSS markers, collision behavior, and accessibility.\u003c/p\u003e\n"]]],["Google's `google.maps.Marker` is deprecated as of February 21st, 2024, in favor of `google.maps.marker.AdvancedMarkerElement`. To migrate, import the marker library, replace `google.maps.Marker` with the new class, and add a map ID to map initialization code (e.g., `mapId: 'DEMO_MAP_ID'`). Load the library dynamically with `google.maps.importLibrary(\"marker\")` or add `libraries=marker` to the script tag. Advanced markers offer enhanced customization, including size, color, and HTML/CSS-based designs.\n"],null,["As of February 21st, 2024 (v3.56), google.maps.Marker is deprecated. We\nencourage you to transition to the new `google.maps.marker.AdvancedMarkerElement`\nclass. Advanced markers provide substantial improvements over the legacy\n`google.maps.Marker` class.\n\n[Learn more about this deprecation](/maps/deprecations#googlemapsmarker_in_the_deprecated_as_of_february_2023)\n\nTo update a legacy marker to be an advanced marker, take the following steps:\n\n1. Add code to import the marker library. Note that the previous version of markers (`google.maps.Marker`) does not have this requirement.\n2. Change `google.maps.Marker` to `google.maps.marker.AdvancedMarkerElement`\n3. Add a map ID to your map initialization code. For example `mapId: 'DEMO_MAP_ID'` for testing purposes if you don't have a map ID already.\n\nAdd the Advanced Marker library\n\nThe method you use for loading libraries depends on how your web page loads the\nMaps JavaScript API.\n\n- If your web page uses dynamic script loading, add the markers library and\n import `AdvancedMarkerElement` (and optionally `PinElement`) at runtime, as\n shown here.\n\n ```javascript\n const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary(\"marker\");\n ```\n- If your web page uses the legacy direct script loading tag, add\n `libraries=marker` to the loading script, as shown in the following snippet.\n\n ```html\n \u003cscript\n src=\"https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&libraries=marker\"\n defer\n \u003e\u003c/script\u003e\n ```\n\n[Learn more about loading the Maps JavaScript API](/maps/documentation/javascript/load-maps-js-api)\n\nExamples\n\nThe following code examples show code for adding a legacy marker, followed by\nthe code for the same example using advanced markers:\n\nBefore migration \n\n```javascript\n// The location of Uluru\nconst position = { lat: -25.344, lng: 131.031 };\n\nconst map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 4,\n center: position,\n});\n\n// The marker, positioned at Uluru\nconst marker = new google.maps.Marker({\n map: map,\n position: position,\n title: 'Uluru',\n});\n```\n\nAfter migration \n\n```javascript\n // The location of Uluru\n const position = { lat: -25.344, lng: 131.031 };\n\n const map = new google.maps.Map(document.getElementById(\"map\"), {\n zoom: 4,\n center: position,\n mapId: \"DEMO_MAP_ID\", // Map ID is required for advanced markers.\n });\n\n // The advanced marker, positioned at Uluru\n const marker = new google.maps.marker.AdvancedMarkerElement({\n map,\n position: position,\n title: 'Uluru',\n });\n```\n\nExplore advanced marker features\n\nAdvanced markers can be customized in ways that were not possible before.\nNow you can adjust the size (scale) of markers, and change the colors of the\nbackground, border, and glyph. Custom graphic images are simpler to work with,\nand it is now possible to compose custom markers using HTML and CSS. Learn more\nabout everything you can do with advanced markers:\n\n- [Basic marker customization](/maps/documentation/javascript/advanced-markers/basic-customization)\n- [Create markers with graphics](/maps/documentation/javascript/advanced-markers/graphic-markers)\n- [Create markers with HTML and CSS](/maps/documentation/javascript/advanced-markers/html-markers)\n- [Control collision behavior, altitude, and visibility](/maps/documentation/javascript/advanced-markers/collision-behavior)\n- [Make markers clickable and accessible](/maps/documentation/javascript/advanced-markers/accessible-markers)"]]