เกริ่นนำ
บทแนะนำนี้จะแสดงวิธีเพิ่ม Google Maps อย่างง่ายด้วยเครื่องหมายลงในหน้าเว็บ เหมาะสำหรับผู้ที่มีความรู้เกี่ยวกับ HTML และ CSS ในระดับเริ่มต้นหรือระดับกลาง และมีความรู้เกี่ยวกับ JavaScript เล็กน้อย สำหรับคำแนะนำขั้นสูงในการสร้างแผนที่ โปรดอ่านคู่มือสำหรับนักพัฒนาซอฟต์แวร์
ด้านล่างนี้คือแผนที่ที่คุณจะสร้างโดยใช้บทแนะนำนี้ เครื่องหมายระบุตำแหน่งอยู่ที่ อูลูรู (หรือที่เรียกว่าเอเยอร์ร็อค) ใน อุทยานแห่งชาติอูลูรู-คาตา ตูตา
TypeScript
// Initialize and add the map let map; async function initMap(): Promise<void> { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary; const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; // The map, centered at Uluru map = new Map( document.getElementById('map') as HTMLElement, { zoom: 4, center: position, mapId: 'DEMO_MAP_ID', } ); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: 'Uluru' }); } initMap();
JavaScript
// Initialize and add the map let map; async function initMap() { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); // The map, centered at Uluru map = new Map(document.getElementById("map"), { zoom: 4, center: position, mapId: "DEMO_MAP_ID", }); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: "Uluru", }); } initMap();
CSS
/* * Always set the map height explicitly to define the size of the div element * that contains the map. */ #map { height: 100%; } /* * Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; }
HTML
<html> <head> <title>Add Map</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <h3>My Google Maps Demo</h3> <!--The div element for the map --> <div id="map"></div> <!-- 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: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script> </body> </html>
ลองใช้ตัวอย่าง
เริ่มต้นใช้งาน
การสร้าง Google แผนที่ที่มีเครื่องหมายกำกับบนหน้าเว็บของคุณมี 3 ขั้นตอนดังนี้
คุณต้องมีเว็บเบราว์เซอร์ เลือกเบราว์เซอร์ที่เป็นที่รู้จัก เช่น Google Chrome (แนะนำ), Firefox, Safari หรือ Edge ตามแพลตฟอร์มของคุณจากรายการเบราว์เซอร์ที่รองรับ
ขั้นตอนที่ 1: สร้างหน้า HTML
โค้ดสำหรับหน้าเว็บ HTML พื้นฐานมีดังนี้
<!doctype html> <!-- @license Copyright 2019 Google LLC. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 --> <html> <head> <title>Add Map</title> <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <h3>My Google Maps Demo</h3> <!--The div element for the map --> <div id="map"></div> <!-- 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: "AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg", v: "weekly"});</script> </body> </html>
โปรดทราบว่าหน้านี้เป็นหน้าเว็บพื้นฐานที่มีส่วนหัวระดับ 3 (h3
) และองค์ประกอบ div
รายการเดียว คุณเพิ่มเนื้อหาใดก็ได้ที่ต้องการลงในหน้าเว็บ
การทำความเข้าใจโค้ด
โค้ดด้านล่างนี้จะสร้างหน้า HTML ซึ่งประกอบด้วยส่วนหัวและส่วนเนื้อหา
<html> <head> </head> <body> </body> </html>
คุณสามารถเพิ่มส่วนหัวระดับที่ 3 เหนือแผนที่ได้โดยใช้โค้ดด้านล่าง
<h3>My Google Maps Demo</h3>
รหัสด้านล่างระบุพื้นที่ของหน้าเว็บสำหรับ Google Maps
<!--The div element for the map --> <div id="map"></div>
ในขั้นตอนนี้ของบทแนะนำ div
จะปรากฏเป็นบล็อกสีเทาเนื่องจากคุณยังไม่ได้เพิ่มแผนที่ โค้ดด้านล่างอธิบาย CSS ที่กำหนดขนาดและสีของ div
/* Set the size of the div element that contains the map */ #map { height: 400px; /* The height is 400 pixels */ width: 100%; /* The width is the width of the web page */ }
ในโค้ดด้านบน องค์ประกอบ style
จะกำหนดขนาด div
สำหรับแผนที่ของคุณ ตั้งค่าความกว้างและความสูงของ div
ให้มากกว่า 0px เพื่อให้แผนที่แสดง ในกรณีนี้ ระบบจะตั้งค่า div
เป็นความสูง 400 พิกเซล และความกว้าง 100% เพื่อแสดงตลอดความกว้างของหน้าเว็บ
Booter Loader จะเตรียม Maps JavaScript API สำหรับการโหลด (ไม่มีการโหลดไลบรารีจนกว่าจะมีการเรียก importLibrary()
)
<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: "YOUR_API_KEY", v: "weekly", // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.). // Add other bootstrap parameters as needed, using camel case. }); </script>
ดูขั้นตอนที่ 3: รับคีย์ API เพื่อดูวิธีรับคีย์ API ของคุณเอง
ขั้นตอนที่ 2: เพิ่มแผนที่ที่มีเครื่องหมาย
ส่วนนี้แสดงวิธีโหลด Maps JavaScript API ลงในหน้าเว็บ และวิธีเขียน JavaScript ของคุณเองที่ใช้ API ดังกล่าวเพื่อเพิ่มแผนที่ที่มีเครื่องหมาย
TypeScript
// Initialize and add the map let map; async function initMap(): Promise<void> { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary; const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; // The map, centered at Uluru map = new Map( document.getElementById('map') as HTMLElement, { zoom: 4, center: position, mapId: 'DEMO_MAP_ID', } ); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: 'Uluru' }); } initMap();
JavaScript
// Initialize and add the map let map; async function initMap() { // The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); // The map, centered at Uluru map = new Map(document.getElementById("map"), { zoom: 4, center: position, mapId: "DEMO_MAP_ID", }); // The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: "Uluru", }); } initMap();
ในโค้ดข้างต้น ไลบรารี Map
และ AdvancedMarkerView
จะโหลดเมื่อมีการเรียกฟังก์ชัน initMap()
การทำความเข้าใจโค้ด
โค้ดด้านล่างจะสร้างวัตถุ Google Maps ใหม่และเพิ่มคุณสมบัติให้แผนที่ ซึ่งรวมถึงระดับจุดศูนย์กลางและระดับการซูม โปรดดูตัวเลือกพร็อพเพอร์ตี้อื่นๆ ในเอกสารประกอบ
TypeScript
// The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps") as google.maps.MapsLibrary; const { AdvancedMarkerElement } = await google.maps.importLibrary("marker") as google.maps.MarkerLibrary; // The map, centered at Uluru map = new Map( document.getElementById('map') as HTMLElement, { zoom: 4, center: position, mapId: 'DEMO_MAP_ID', } );
JavaScript
// The location of Uluru const position = { lat: -25.344, lng: 131.031 }; // Request needed libraries. //@ts-ignore const { Map } = await google.maps.importLibrary("maps"); const { AdvancedMarkerElement } = await google.maps.importLibrary("marker"); // The map, centered at Uluru map = new Map(document.getElementById("map"), { zoom: 4, center: position, mapId: "DEMO_MAP_ID", });
ในโค้ดข้างต้น new Map()
จะสร้างวัตถุใหม่ใน Google Maps พร็อพเพอร์ตี้ center
จะบอก API ว่าควรตั้งศูนย์กลางแผนที่ไว้ที่ใด
เรียนรู้เพิ่มเติมเกี่ยวกับการทราบพิกัดละติจูด/ลองจิจูด หรือการแปลงที่อยู่เป็นพิกัดทางภูมิศาสตร์
คุณสมบัติ zoom
ระบุระดับการซูมสำหรับแผนที่ ซูม: 0 คือการซูมที่ต่ำที่สุด
และแสดงทั้งโลก ตั้งค่าการซูมให้สูงขึ้นเพื่อซูมเข้า
ให้โลกด้วยความละเอียดสูง
โค้ดด้านล่างจะระบุเครื่องหมายบนแผนที่ พร็อพเพอร์ตี้ position
จะกำหนดตำแหน่งของเครื่องหมาย
TypeScript
// The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: 'Uluru' });
JavaScript
// The marker, positioned at Uluru const marker = new AdvancedMarkerElement({ map: map, position: position, title: "Uluru", });
ดูข้อมูลเพิ่มเติมเกี่ยวกับเครื่องหมาย:
ขั้นตอนที่ 3: รับคีย์ API
ส่วนนี้จะอธิบายวิธีตรวจสอบสิทธิ์แอปกับ Maps JavaScript API โดยใช้คีย์ API ของคุณเอง
ทำตามขั้นตอนต่อไปนี้เพื่อรับคีย์ API
ไปที่ Google Cloud Console
สร้างหรือเลือกโปรเจ็กต์
คลิกต่อไปเพื่อเปิดใช้ API และบริการที่เกี่ยวข้อง
ในหน้าข้อมูลเข้าสู่ระบบ ให้รับคีย์ API (และตั้งค่าข้อจำกัดคีย์ API) หมายเหตุ: หากมีคีย์ API แบบไม่จํากัดอยู่แล้วหรือคีย์ที่มีข้อจํากัดของเบราว์เซอร์ คุณอาจใช้คีย์นั้นได้
เพื่อป้องกันการขโมยโควต้าและรักษาความปลอดภัยของคีย์ API โปรดดูการใช้คีย์ API
เปิดใช้การเรียกเก็บเงิน ดูข้อมูลเพิ่มเติมได้ที่การใช้งานและการเรียกเก็บเงิน
คัดลอกโค้ดทั้งหมดของบทแนะนำนี้จากหน้านี้ไปยังเครื่องมือแก้ไขข้อความ
แทนที่ค่าของพารามิเตอร์
key
ใน URL ด้วยคีย์ API ของคุณเอง (ซึ่งก็คือคีย์ API ที่คุณเพิ่งได้รับ)<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: "YOUR_API_KEY", v: "weekly", // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.). // Add other bootstrap parameters as needed, using camel case. }); </script>
บันทึกไฟล์นี้โดยใช้ชื่อที่ลงท้ายด้วย
.html
เช่นindex.html
โหลดไฟล์ HTML ในเว็บเบราว์เซอร์โดยลากไฟล์จากเดสก์ท็อปไปไว้ในเบราว์เซอร์ การดับเบิลคลิกไฟล์ใช้ได้กับระบบปฏิบัติการส่วนใหญ่
เคล็ดลับและการแก้ปัญหา
- คุณสามารถปรับแต่งตัวเลือกต่างๆ เช่น รูปแบบและคุณสมบัติ เพื่อปรับแต่งแผนที่ได้ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการปรับแต่งแผนที่ โปรดอ่านคำแนะนำเกี่ยวกับการจัดรูปแบบและการวาดบนแผนที่
- ใช้คอนโซลเครื่องมือสำหรับนักพัฒนาซอฟต์แวร์ในเว็บเบราว์เซอร์เพื่อทดสอบและเรียกใช้โค้ด อ่านรายงานข้อผิดพลาด และแก้ไขปัญหาเกี่ยวกับโค้ด
- ใช้แป้นพิมพ์ลัดต่อไปนี้เพื่อเปิดคอนโซลใน Chrome
Command+Option+J (ใน Mac) หรือ Control+Shift+J (ใน Windows) ทำตามขั้นตอนด้านล่างเพื่อรับพิกัดละติจูดและลองจิจูดของสถานที่บน Google Maps
- เปิด Google Maps ในเบราว์เซอร์
- คลิกขวาที่ตำแหน่งที่แน่นอนบนแผนที่ที่คุณต้องการพิกัด
- เลือกที่นี่คือที่ไหนจากเมนูตามบริบทที่ปรากฏขึ้น แผนที่จะแสดงการ์ดที่ด้านล่างของหน้าจอ ค้นหาพิกัดละติจูดและลองจิจูดในแถวสุดท้ายของการ์ด
คุณสามารถแปลงที่อยู่เป็นพิกัดละติจูดและลองจิจูดโดยใช้บริการการระบุพิกัดทางภูมิศาสตร์ คู่มือนักพัฒนาซอฟต์แวร์จะให้ข้อมูลโดยละเอียดเกี่ยวกับการเริ่มต้นใช้งานบริการการระบุพิกัดทางภูมิศาสตร์