Di chuyển sang điểm đánh dấu nâng cao
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Kể từ ngày 21 tháng 2 năm 2024 (phiên bản 3.56), google.maps.Marker sẽ ngừng hoạt động. Bạn nên chuyển sang lớp google.maps.marker.AdvancedMarkerElement
mới. Các điểm đánh dấu nâng cao mang lại những cải tiến đáng kể so với lớp google.maps.Marker
cũ.
Tìm hiểu thêm về việc ngừng sử dụng này
Để cập nhật một điểm đánh dấu cũ thành điểm đánh dấu nâng cao, hãy làm theo các bước sau:
- Thêm mã để nhập thư viện điểm đánh dấu. Xin lưu ý rằng phiên bản trước của điểm đánh dấu (
google.maps.Marker
) không có yêu cầu này.
- Thay đổi
google.maps.Marker
thành google.maps.marker.AdvancedMarkerElement
- Thêm mã nhận dạng bản đồ vào mã khởi chạy bản đồ. Ví dụ:
mapId: 'DEMO_MAP_ID'
cho mục đích kiểm thử nếu bạn chưa có mã bản đồ.
Thêm thư viện Advanced Marker
Phương thức bạn dùng để tải thư viện phụ thuộc vào cách trang web của bạn tải API JavaScript của Maps.
Nếu trang web của bạn sử dụng tính năng tải tập lệnh động, hãy thêm thư viện điểm đánh dấu và nhập AdvancedMarkerElement
(và tuỳ ý PinElement
) trong thời gian chạy, như minh hoạ ở đây.
const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker");
Nếu trang web của bạn sử dụng thẻ tải tập lệnh trực tiếp cũ, hãy thêm libraries=marker
vào tập lệnh tải, như minh hoạ trong đoạn mã sau.
<script
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&libraries=marker"
defer
></script>
Tìm hiểu thêm về cách tải Maps JavaScript API
Ví dụ
Các ví dụ về mã sau đây cho thấy mã để thêm một điểm đánh dấu cũ, theo sau là mã cho cùng một ví dụ bằng cách sử dụng điểm đánh dấu nâng cao:
Trước khi di chuyển
// 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',
});
Sau khi di chuyển
// 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',
});
Khám phá các tính năng nâng cao của điểm đánh dấu
Bạn có thể tuỳ chỉnh các điểm đánh dấu nâng cao theo những cách mà trước đây không thể thực hiện được.
Giờ đây, bạn có thể điều chỉnh kích thước (tỷ lệ) của điểm đánh dấu và thay đổi màu sắc của nền, đường viền và ký tự. Bạn có thể dễ dàng làm việc với hình ảnh đồ hoạ tuỳ chỉnh và giờ đây, bạn có thể tạo các điểm đánh dấu tuỳ chỉnh bằng HTML và CSS. Tìm hiểu thêm về mọi việc bạn có thể làm với điểm đánh dấu nâng cao:
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-17 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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)"]]