ภาพรวม
เรียนรู้วิธีนำเข้าข้อมูล GeoJSON จากต้นทางในเครื่องหรือระยะไกล และแสดงข้อมูลนั้นบนแผนที่ของคุณ บทแนะนำนี้ใช้แผนที่ด้านล่างในการแสดงให้เห็นเทคนิคต่างๆ ในการนำเข้าข้อมูลไปยังแผนที่
ส่วนด้านล่างแสดงโค้ดทั้งหมดที่คุณต้องสร้างแผนที่ในบทแนะนำนี้
TypeScript
let map: google.maps.Map; function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 2, center: new google.maps.LatLng(2.8, -187.3), mapTypeId: "terrain", }); // Create a <script> tag and set the USGS URL as the source. const script = document.createElement("script"); // This example uses a local copy of the GeoJSON stored at // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp script.src = "https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js"; document.getElementsByTagName("head")[0].appendChild(script); } // Loop through the results array and place a marker for each // set of coordinates. const eqfeed_callback = function (results: any) { for (let i = 0; i < results.features.length; i++) { const coords = results.features[i].geometry.coordinates; const latLng = new google.maps.LatLng(coords[1], coords[0]); new google.maps.Marker({ position: latLng, map: map, }); } }; declare global { interface Window { initMap: () => void; eqfeed_callback: (results: any) => void; } } window.initMap = initMap; window.eqfeed_callback = eqfeed_callback;
JavaScript
let map; function initMap() { map = new google.maps.Map(document.getElementById("map"), { zoom: 2, center: new google.maps.LatLng(2.8, -187.3), mapTypeId: "terrain", }); // Create a <script> tag and set the USGS URL as the source. const script = document.createElement("script"); // This example uses a local copy of the GeoJSON stored at // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp script.src = "https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js"; document.getElementsByTagName("head")[0].appendChild(script); } // Loop through the results array and place a marker for each // set of coordinates. const eqfeed_callback = function (results) { for (let i = 0; i < results.features.length; i++) { const coords = results.features[i].geometry.coordinates; const latLng = new google.maps.LatLng(coords[1], coords[0]); new google.maps.Marker({ position: latLng, map: map, }); } }; window.initMap = initMap; window.eqfeed_callback = eqfeed_callback;
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>Earthquake Markers</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> <div id="map"></div> <!-- The `defer` attribute causes the script to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly" defer ></script> </body> </html>
ลองใช้ตัวอย่าง
กำลังโหลดข้อมูล
ส่วนนี้จะแสดงวิธีโหลดข้อมูลจากโดเมนเดียวกับแอปพลิเคชัน Maps JavaScript API หรือจากโดเมนอื่น
กำลังโหลดข้อมูลจากโดเมนเดียวกัน
ชั้นข้อมูลของ Google Maps มีคอนเทนเนอร์สำหรับข้อมูลทางภูมิศาสตร์ที่กำหนดเอง (รวมถึง GeoJSON) หากข้อมูลของคุณอยู่ในไฟล์ที่โฮสต์บนโดเมนเดียวกันกับแอปพลิเคชัน Maps JavaScript API คุณสามารถโหลดได้โดยใช้เมธอด map.data.loadGeoJson()
ไฟล์ต้องอยู่ในโดเมนเดียวกัน
แต่คุณจะโฮสต์ไฟล์ดังกล่าวในโดเมนย่อยอื่นได้ เช่น คุณส่งคำขอไปยัง files.example.com จาก www.example.com ได้
map.data.loadGeoJson('data.json');
กำลังโหลดข้อมูลข้ามโดเมน
นอกจากนี้ คุณยังขอข้อมูลจากโดเมนอื่นที่ไม่ใช่โดเมนของคุณได้อีกด้วย หากการกำหนดค่าของโดเมนอนุญาตคำขอดังกล่าว มาตรฐานสำหรับสิทธิ์นี้เรียกว่า การแชร์ทรัพยากรข้ามต้นทาง (CORS) หากโดเมนอนุญาตคำขอข้ามโดเมน ส่วนหัวการตอบกลับของโดเมนควรมีการประกาศต่อไปนี้
Access-Control-Allow-Origin: *
ใช้เครื่องมือสำหรับนักพัฒนาซอฟต์แวร์ Chrome (เครื่องมือสำหรับนักพัฒนาเว็บ) เพื่อดูว่าโดเมนเปิดใช้ CORS หรือไม่
การโหลดข้อมูลจากโดเมนดังกล่าวก็จะเหมือนกับการโหลด JSON จากโดเมนเดียวกัน
map.data.loadGeoJson('http://www.CORS-ENABLED-SITE.com/data.json');
กำลังขอ JSONP
โดเมนเป้าหมายต้องรองรับคำขอสำหรับ JSONP จึงจะใช้เทคนิคนี้ได้
หากต้องการขอ JSONP ให้ใช้ createElement()
เพื่อเพิ่มแท็ก script
ในส่วนหัวของเอกสาร
var script = document.createElement('script');
script.src = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp';
document.getElementsByTagName('head')[0].appendChild(script);
เมื่อสคริปต์ทำงาน โดเมนเป้าหมายจะส่งข้อมูลเป็นอาร์กิวเมนต์ไปยังสคริปต์อื่น ซึ่งโดยปกติจะมีชื่อว่า callback()
โดเมนเป้าหมายจะกำหนดชื่อสคริปต์ Callback ซึ่งเป็นชื่อในหน้าเมื่อคุณโหลด URL เป้าหมายในเบราว์เซอร์
ตัวอย่างเช่น โหลด
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
ในหน้าต่างเบราว์เซอร์เพื่อแสดงชื่อ Callback เป็น eqfeed_callback
คุณต้องกำหนดสคริปต์ Callback ในโค้ดของคุณดังนี้
function eqfeed_callback(response) {
map.data.addGeoJson(response);
}
ใช้เมธอด addGeoJson()
เพื่อวางข้อมูล GeoJSON ที่แยกวิเคราะห์ไว้บนแผนที่
การจัดรูปแบบข้อมูล
คุณสามารถเปลี่ยนลักษณะที่ปรากฏของข้อมูลได้โดยเพิ่มข้อมูล GeoJSON ลงในออบเจ็กต์แผนที่ อ่านคู่มือนักพัฒนาซอฟต์แวร์เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับการจัดรูปแบบข้อมูล
ดูข้อมูลเพิ่มเติม
- GeoJSON เป็นรูปแบบเปิดที่ใช้กันอย่างแพร่หลายในการเข้ารหัสข้อมูลทางภูมิศาสตร์โดยอิงตาม JSON (JavaScript Object Notation) เครื่องมือและเมธอด JavaScript ที่ออกแบบสำหรับข้อมูล JSON ยังใช้งานกับ GeoJSON ได้ด้วย อ่านข้อมูลเพิ่มเติมได้ในคู่มือสำหรับนักพัฒนาซอฟต์แวร์
- JSONP ย่อมาจาก padded JSON ซึ่งเป็นวิธีการสื่อสารที่ใช้ในโปรแกรม JavaScript ที่ทำงานในเว็บเบราว์เซอร์เพื่อขอข้อมูลจากเซิร์ฟเวอร์ในโดเมนอื่น