ใช้เครื่องหมายเพื่อแสดงสถานที่เดียวบนแผนที่ หน้านี้แสดงวิธีเพิ่มเครื่องหมายลงในแผนที่แบบเป็นโปรแกรมและโดยใช้องค์ประกอบ HTML ที่กำหนดเอง
โหลดไลบรารีเครื่องหมายขั้นสูง
หากต้องการเพิ่มเครื่องหมายขั้นสูงลงในแผนที่ โค้ดแผนที่ต้องโหลดไลบรารี
marker ซึ่งมีคลาส AdvancedMarkerElement และ PinElement โดยจะมีผลไม่ว่าแอปจะโหลดเครื่องหมายโดยใช้โปรแกรม
หรือใช้ HTML หากต้องการทำเช่นนี้ แอปของคุณต้องโหลด Maps JavaScript API ก่อน
วิธีการโหลดไลบรารีจะขึ้นอยู่กับวิธีที่หน้าเว็บโหลด Maps JavaScript API
หากหน้าเว็บใช้การโหลดสคริปต์แบบไดนามิก ให้เพิ่มไลบรารีเครื่องหมายและ นำเข้า
AdvancedMarkerElement(และPinElementหากต้องการ) ในรันไทม์ตามที่ แสดงที่นี่const { AdvancedMarkerElement, PinElement } = await google.maps.importLibrary("marker");
หากหน้าเว็บใช้แท็กการโหลดสคริปต์โดยตรง ให้เพิ่ม
libraries=markerไปยัง สคริปต์การโหลด ดังที่แสดงในข้อมูลโค้ดต่อไปนี้ การดำเนินการนี้จะทำให้ระบบนำเข้าทั้งAdvancedMarkerElementและPinElement<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&libraries=marker" defer ></script>
ตั้งค่ารหัสแผนที่
ต้องระบุรหัสแผนที่เพื่อใช้เครื่องหมายขั้นสูง (ใช้ DEMO_MAP_ID ได้)
ตั้งค่ารหัสแผนที่ในตัวเลือกแผนที่ ดังที่แสดงที่นี่
const map = new Map(document.getElementById('map') as HTMLElement, { center: { lat: 37.4239163, lng: -122.0947209 }, zoom: 14, mapId: 'DEMO_MAP_ID', });
หากใช้คอมโพเนนต์ของเว็บ คุณจะตั้งค่ารหัสแผนที่ในองค์ประกอบ gmp-map
ได้โดยตรง
<gmp-map center="37.4239163,-122.0947209" zoom="14" map-id="DEMO_MAP_ID"></gmp-map>
ดูข้อมูลเพิ่มเติมเกี่ยวกับรหัสแผนที่
เพิ่มตัวทำเครื่องหมายโดยใช้องค์ประกอบ HTML ที่กำหนดเอง
หากต้องการเพิ่มเครื่องหมายขั้นสูงโดยใช้องค์ประกอบ HTML ที่กำหนดเอง ให้เพิ่มองค์ประกอบย่อย gmp-advanced-marker ลงในองค์ประกอบ gmp-map ข้อมูลโค้ดต่อไปนี้
แสดงการเพิ่มเครื่องหมายลงในหน้าเว็บ
<gmp-map
center="41.027748173921374, -92.41852445367961"
zoom="13"
map-id="DEMO_MAP_ID">
<gmp-advanced-marker
position="41.027748173921374, -92.41852445367961"
title="Ottumwa, IA"></gmp-advanced-marker>
</gmp-map>ดูซอร์สโค้ดตัวอย่างที่สมบูรณ์
ตัวอย่างนี้แสดงการสร้างแผนที่ที่มีเครื่องหมายโดยใช้ HTML
TypeScript
// This example adds a map with markers, using web components. async function initMap() { console.log('Maps JavaScript API loaded.'); } initMap();
JavaScript
// This example adds a map with markers, using web components. async function initMap() { console.log('Maps JavaScript API loaded.'); } initMap();
CSS
/* Note: This CSS file is intentionally blank. */
HTML
<html>
<head>
<title>Add a Map with Markers using HTML</title>
<style>
gmp-map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script type="module" src="./index.js"></script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8&libraries=maps,marker&v=weekly&internal_usage_attribution_ids=gmp_git_jsapisamples_v1_web-components"
defer></script>
</head>
<body>
<gmp-map
center="41.027748173921374, -92.41852445367961"
zoom="13"
map-id="DEMO_MAP_ID">
<gmp-advanced-marker
position="41.027748173921374, -92.41852445367961"
title="Ottumwa, IA"></gmp-advanced-marker>
</gmp-map>
</body>
</html>ลองใช้ตัวอย่าง
เพิ่มเครื่องหมายโดยใช้โปรแกรม
หากต้องการเพิ่มเครื่องหมายขั้นสูงลงในแผนที่โดยใช้โปรแกรม ให้สร้าง AdvancedMarkerElement ใหม่และต่อท้ายแผนที่ตามที่แสดงในตัวอย่างนี้
TypeScript
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; async function initMap() { // Request needed libraries. const { Map } = (await google.maps.importLibrary( 'maps' )) as google.maps.MapsLibrary; const { AdvancedMarkerElement } = (await google.maps.importLibrary( 'marker' )) as google.maps.MarkerLibrary; const marker = new AdvancedMarkerElement({ position: { lat: 37.4239163, lng: -122.0947209 }, }); mapElement.append(marker); }
JavaScript
const mapElement = document.querySelector('gmp-map'); async function initMap() { // Request needed libraries. const { Map } = (await google.maps.importLibrary('maps')); const { AdvancedMarkerElement } = (await google.maps.importLibrary('marker')); const marker = new AdvancedMarkerElement({ position: { lat: 37.4239163, lng: -122.0947209 }, }); mapElement.append(marker); }
การต่อท้ายองค์ประกอบจะทำได้เมื่อใช้คอมโพเนนต์เว็บเท่านั้น หากใช้องค์ประกอบ div เพื่อโหลดแผนที่ ให้ใช้พร็อพเพอร์ตี้ map เพื่อเชื่อมโยงเครื่องหมายกับอินสแตนซ์ของแผนที่ตามที่แสดงไว้ที่นี่
myMap = new google.maps.Map(document.getElementById("map"), {
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
});
const marker = new AdvancedMarkerElement({
map: myMap,
position: { lat: -34.397, lng: 150.644 },
});นำเครื่องหมายออก
หากต้องการนำเครื่องหมายออกจากแผนที่ ให้ตั้งค่า marker.map หรือ marker.position เป็น
null
// Set the map to null. marker.map = null; // Set the position to null. marker.position = null;
ดูซอร์สโค้ดตัวอย่างที่สมบูรณ์
ตัวอย่างนี้แสดงวิธีเพิ่มเครื่องหมายลงในแผนที่
TypeScript
const mapElement = document.querySelector('gmp-map') as google.maps.MapElement; async function initMap() { // Request needed libraries. const { Map } = (await google.maps.importLibrary( 'maps' )) as google.maps.MapsLibrary; const { AdvancedMarkerElement } = (await google.maps.importLibrary( 'marker' )) as google.maps.MarkerLibrary; const marker = new AdvancedMarkerElement({ position: { lat: 37.4239163, lng: -122.0947209 }, }); mapElement.append(marker); } initMap();
JavaScript
const mapElement = document.querySelector('gmp-map'); async function initMap() { // Request needed libraries. const { Map } = (await google.maps.importLibrary('maps')); const { AdvancedMarkerElement } = (await google.maps.importLibrary('marker')); const marker = new AdvancedMarkerElement({ position: { lat: 37.4239163, lng: -122.0947209 }, }); mapElement.append(marker); } initMap();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ gmp-map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html>
<head>
<title>Default Advanced Marker</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<!-- prettier-ignore -->
<script>(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})
({key: "AIzaSyA6myHzS10YXdcazAFalmXvDkrYCp5cLc8", v: "weekly" });</script>
</head>
<body>
<gmp-map
center="37.4239163,-122.0947209"
zoom="14"
map-id="4504f8b37365c3d0"></gmp-map>
</body>
</html>