Sử dụng điểm đánh dấu để hiển thị một vị trí trên bản đồ. Trang này trình bày cách thêm điểm đánh dấu vào bản đồ theo phương thức lập trình và bằng cách sử dụng HTML.
Thêm điểm đánh dấu bằng HTML
Để thêm điểm đánh dấu 3D bằng HTML, hãy thêm một phần tử con gmp-marker-3d vào phần tử gmp-map-3d. Đoạn mã sau đây cho thấy cách thêm điểm đánh dấu vào trang web:
<html>
<head>
<title>3D Marker HTML</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script
async
src="https://maps.googleapis.com/maps/api/js?loading=async&key=GOOGLE_MAPS_API_KEY&libraries=maps3d"></script>
</head>
<body>
<gmp-map-3d
center="40.7489,-73.9680,0"
heading="315"
tilt="65"
range="800"
mode="SATELLITE">
<gmp-marker position="40.7489,-73.9680" title="UN Headquarters">
<div class="custom-marker">
United Nations Secretariat Building
</div>
</gmp-marker>
</gmp-map-3d>
</body>
</html>Thêm điểm đánh dấu theo phương thức lập trình
Để thêm điểm đánh dấu 3D vào bản đồ theo phương thức lập trình, hãy tạo một Marker3DElement mới, truyền toạ độ lat/lng và tham chiếu đến bản đồ cơ sở, như trong ví dụ sau:
async function init() { // Make sure the Marker3DElement is included. const { Map3DElement, Marker3DElement } = await google.maps.importLibrary('maps3d'); const map = new Map3DElement({ center: { lat: 37.4239163, lng: -122.0947209, altitude: 0 }, tilt: 67.5, range: 1000, mode: 'SATELLITE', }); const marker = new Marker3DElement({ position: { lat: 37.4239163, lng: -122.0947209, altitude: 50 }, // (Required) Marker must have a lat / lng, but doesn't need an altitude. altitudeMode: 'ABSOLUTE', // (Optional) Treated as CLAMP_TO_GROUND if omitted. extruded: true, // (Optional) Draws line from ground to the bottom of the marker. label: 'Basic Marker', // (Optional) Add a label to the marker. }); map.append(marker); // The marker must be appended to the map. document.body.append(map); } void init();