ข้อมูลเบื้องต้น
บทแนะนํานี้จะแสดงวิธีเพิ่มแผนที่แบบง่ายๆ ลงใน Google ด้วยเครื่องหมายในหน้าเว็บ เหมาะสําหรับผู้ที่มีความรู้เบื้องต้นหรือระดับกลางเกี่ยวกับ HTML และ CSS และมีความรู้ด้าน JavaScript เล็กน้อย สําหรับคู่มือขั้นสูงในการสร้างแผนที่ โปรดอ่านคู่มือสําหรับนักพัฒนาซอฟต์แวร์
ด้านล่างนี้คือแผนที่ที่คุณจะสร้างโดยใช้บทแนะนํานี้ ตัวทําเครื่องหมายวางอยู่ในตําแหน่งที่ Uluru (หรือที่รู้จักกันในชื่อ Ayers Rock) ในอุทยานแห่งชาติ Uluru-Kata Tjuta
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 { AdvancedMarkerView } = 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 AdvancedMarkerView({ 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 { AdvancedMarkerView } = 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 AdvancedMarkerView({ 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 Maps ด้วยตัวทําเครื่องหมายในหน้าเว็บมี 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>
คุณสามารถเพิ่มระดับหัวเรื่องสามระดับเหนือแผนที่โดยใช้รหัสด้านล่าง
<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% เพื่อแสดงความกว้างของหน้าเว็บ
ตัวโหลดรองเท้าบู๊ตจะเตรียม 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_HERE", // Add other bootstrap parameters as needed, using camel case. // Use the 'v' parameter to indicate the version to load (alpha, beta, weekly, etc.) }); </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 { AdvancedMarkerView } = 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 AdvancedMarkerView({ 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 { AdvancedMarkerView } = 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 AdvancedMarkerView({ 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 { AdvancedMarkerView } = 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 { AdvancedMarkerView } = 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 คือการซูมต่ําสุด
และแสดงโลกทั้งใบ ตั้งค่าการซูมให้สูงขึ้นเพื่อซูมเข้า
Earth ที่ความละเอียดที่สูงขึ้น
โค้ดด้านล่างจะแสดงเครื่องหมายบนแผนที่ พร็อพเพอร์ตี้ position
จะกําหนดตําแหน่งของเครื่องหมาย
TypeScript
// The marker, positioned at Uluru const marker = new AdvancedMarkerView({ map: map, position: position, title: 'Uluru' });
JavaScript
// The marker, positioned at Uluru const marker = new AdvancedMarkerView({ 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_HERE", // Add other bootstrap parameters as needed, using camel case. // Use the 'v' parameter to indicate the version to load (alpha, beta, weekly, etc.) }); </script>
บันทึกไฟล์นี้ซึ่งลงท้ายด้วย
.html
เช่นindex.html
โหลดไฟล์ HTML ในเว็บเบราว์เซอร์โดยลากไฟล์จากเดสก์ท็อปไปไว้ในเบราว์เซอร์ หรือดับเบิลคลิกไฟล์ที่ต้องการในระบบปฏิบัติการส่วนใหญ่
เคล็ดลับและการแก้ปัญหา
- คุณสามารถปรับเปลี่ยนตัวเลือกต่างๆ เช่น รูปแบบและคุณสมบัติ เพื่อปรับแต่งแผนที่ได้ สําหรับข้อมูลเพิ่มเติมเกี่ยวกับการปรับแต่งแผนที่ ให้อ่านคู่มือเกี่ยวกับการจัดรูปแบบ และการวาดบนแผนที่
- ใช้คอนโซลเครื่องมือสําหรับนักพัฒนาซอฟต์แวร์ในเว็บเบราว์เซอร์เพื่อทดสอบและเรียกใช้โค้ด อ่านรายงานข้อผิดพลาด และแก้ปัญหาเกี่ยวกับโค้ด
- ใช้แป้นพิมพ์ลัดต่อไปนี้เพื่อเปิดคอนโซลใน Chrome
Command+Option+J (ใน Mac) หรือ Control+Shift+J (ใน Windows) ทําตามขั้นตอนด้านล่างเพื่อรับพิกัดละติจูดและลองจิจูดสําหรับสถานที่ใน Google Maps
- เปิด Google Maps ในเบราว์เซอร์
- คลิกขวาตําแหน่งที่แน่นอนในแผนที่ที่คุณต้องการพิกัด
- เลือกมีอะไรอยู่จากเมนูตามบริบทที่ปรากฏขึ้น แผนที่จะแสดงการ์ดที่ด้านล่างของหน้าจอ ค้นหาพิกัดละติจูดและลองจิจูดในแถวสุดท้ายของการ์ด
คุณแปลงที่อยู่เป็นพิกัดละติจูดและลองจิจูดได้โดยใช้บริการการเข้ารหัสพิกัดภูมิศาสตร์ คู่มือนักพัฒนาซอฟต์แวร์จะให้ข้อมูลโดยละเอียดเกี่ยวกับการเริ่มต้นใช้งานบริการการเข้ารหัสพิกัดภูมิศาสตร์