หากต้องการโหลดโค้ด JavaScript สําหรับ Maps JavaScript API ให้ใส่สคริปต์ตัวโหลด Boottrap ในหน้าเว็บโดยใช้รูปแบบต่อไปนี้
<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>
Maps JavaScript API ประกอบด้วยไลบรารีที่ไม่ได้โหลดจนกว่าคุณจะขออย่างเจาะจง การแบ่งคอมโพเนนต์เป็นไลบรารีจะช่วยให้ API โหลด (และแยกวิเคราะห์) ได้อย่างรวดเร็ว คุณจะมีค่าใช้จ่ายเพิ่มเติมในการโหลดไลบรารีและแยกวิเคราะห์ไลบรารีเมื่อจําเป็นต้องใช้
โหลดไลบรารีเพิ่มเติมขณะรันไทม์โดยใช้โอเปอเรเตอร์ await
เพื่อเรียก importLibrary()
จากภายในฟังก์ชัน async
ตัวอย่างเช่น
const { Map } = await google.maps.importLibrary("maps");
ตัวอย่างโค้ดต่อไปนี้แสดงการโหลดทั้งไลบรารี Map
และ AdvancedMarkerView
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();
ไลบรารีสําหรับการนําเข้าไลบรารีแบบไดนามิก
ไลบรารีต่อไปนี้ใช้ได้กับการนําเข้าไลบรารีแบบไดนามิก
core
(google.maps.CoreLibrary
)maps
(google.maps.MapsLibrary
)places
(google.maps.PlacesLibrary
)geocoding
(google.maps.GeocodingLibrary
)routes
(google.maps.RoutesLibrary
)marker
(google.maps.MarkerLibrary
)geometry
(google.maps.GeometryLibrary
)elevation
(google.maps.ElevationLibrary
)streetView
(google.maps.StreetViewLibrary
)journeySharing
(google.maps.JourneySharingLibrary
)drawing
(google.maps.DrawingLibrary
)visualization
(google.maps.VisualizationLibrary
)
ไลบรารีสําหรับ URL รองเท้าบู๊ต (เดิม)
ไลบรารีต่อไปนี้รองรับการใช้กับแท็กสคริปต์รองเท้าบู๊ตแร็พเดิมdrawing
มีอินเทอร์เฟซที่เป็นกราฟิกให้ผู้ใช้วาดรูปหลายเหลี่ยม สี่เหลี่ยมผืนผ้า เส้นประกอบ วงกลม และเครื่องหมายบนแผนที่ ดูเอกสารประกอบเกี่ยวกับคลังภาพวาดเพื่อดูข้อมูลเพิ่มเติมgeometry
มีฟังก์ชันยูทิลิตีสําหรับคํานวณค่าเรขาคณิตแบบสเกลาร์ (เช่น ระยะทางและพื้นที่) บนพื้นผิวโลก ดูข้อมูลเพิ่มเติมได้ในเอกสารเกี่ยวกับเรขาคณิตเรขาคณิตjourneySharing
ให้การสนับสนุนสําหรับโซลูชันการขนส่งและโลจิสติกส์ของ Google Maps PlatformlocalContext
แสดงสถานที่น่าสนใจที่สําคัญใกล้ สถานที่ที่คุณระบุ ดูข้อมูลเพิ่มเติมได้จากเอกสารประกอบของไลบรารีบริบทในพื้นที่marker
ช่วยให้คุณเพิ่มตัวทําเครื่องหมายขั้นสูงที่มีประสิทธิภาพและทํางานได้ดีใน Maps ได้ ดูข้อมูลเพิ่มเติมได้จากเอกสารประกอบเกี่ยวกับตัวทําเครื่องหมายขั้นสูงplaces
ช่วยให้แอปพลิเคชันของคุณค้นหาสถานที่ เช่น สถานประกอบการ สถานที่ตั้งทางภูมิศาสตร์ หรือจุดสนใจภายในพื้นที่ที่กําหนดไว้ ดูข้อมูลเพิ่มเติมในเอกสารประกอบของคลังสถานที่visualization
มีแผนที่ความหนาแน่นสําหรับการแสดงภาพข้อมูล อ่านข้อมูลเพิ่มเติมได้ในเอกสารประกอบเกี่ยวกับคลังภาพ
คําขอ Boottrap ต่อไปนี้แสดงวิธีเพิ่มคําขอไลบรารี google.maps.geometry
ของ Maps JavaScript API ลงในสคริปต์ตัวโหลด Bootsrap เดิม
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry&callback=initMap">
</script>
หากต้องการขอไลบรารีหลายรายการ ให้คั่นด้วยคอมมา ดังนี้
<script async
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry,places&callback=initMap">
</script>