ภาพรวม
บทแนะนำนี้จะแสดงวิธีแสดงภาพข้อมูลใน Google Maps ตัวอย่างเช่น แผนที่ในบทแนะนำนี้แสดงภาพข้อมูลเกี่ยวกับตำแหน่งและขนาดของแผ่นดินไหว เรียนรู้เทคนิคที่จะใช้กับแหล่งข้อมูลของคุณเอง และสร้างเรื่องราวที่ทรงพลังบนแผนที่ Google ดังเช่นเรื่องราวด้านล่างนี้
2 เฟรมแรกที่เห็นด้านบน (จากซ้ายไปขวา) แสดงแผนที่ที่มีเครื่องหมายพื้นฐานและวงกลมขนาด เฟรมสุดท้ายจะแสดงแผนที่ความหนาแน่น
การนำเข้าข้อมูล
บทแนะนำนี้ใช้ข้อมูลแผ่นดินไหวแบบเรียลไทม์จากสำนักงานสำรวจทางธรณีวิทยาแห่งสหรัฐอเมริกา (USGS) เว็บไซต์ USGS ให้ข้อมูลในรูปแบบที่หลากหลาย ซึ่งคุณสามารถคัดลอกไปยังโดเมนสำหรับการเข้าถึงในเครื่องจากแอปพลิเคชัน บทแนะนำนี้จะขอ JSONP โดยตรงจากเซิร์ฟเวอร์ USGS โดยเพิ่มแท็ก script
ที่ส่วนหัวของเอกสาร
// Create a script tag and set the USGS URL as the source. 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);
การวางเครื่องหมายพื้นฐาน
ตอนนี้คุณได้ดึงข้อมูลเกี่ยวกับตำแหน่งที่เกิดแผ่นดินไหวจากฟีด USGS มาไว้ในแอปพลิเคชันของคุณแล้ว คุณจะแสดงข้อมูลนั้นบนแผนที่ได้ ส่วนนี้แสดงวิธีสร้างแผนที่ที่ใช้ข้อมูลที่นำเข้าเพื่อวางเครื่องหมายพื้นฐานที่จุดศูนย์กลางของแผ่นดินไหวทุกๆ ตำแหน่ง
ส่วนด้านล่างจะแสดงโค้ดทั้งหมดที่คุณต้องสร้างแผนที่ในบทแนะนำนี้
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 callback 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>
ลองใช้ตัวอย่าง
การใช้รูปร่างและแผนที่ความร้อนเพื่อปรับแต่งแผนที่
ส่วนนี้จะแสดงวิธีอื่นๆ ในการปรับแต่งชุดข้อมูลที่สมบูรณ์บนแผนที่ ลองพิจารณาแผนที่ที่สร้างในส่วนก่อนหน้าของบทแนะนำนี้ ซึ่งจะแสดงเครื่องหมายบนตำแหน่งที่เกิดแผ่นดินไหวทุกแห่ง คุณสามารถปรับแต่งเครื่องหมายเพื่อให้เห็นภาพข้อมูลเพิ่มเติม เช่น ตำแหน่งที่เกิดแผ่นดินไหวมากที่สุด รวมถึงขนาดหรือความลึก
ต่อไปนี้เป็นตัวเลือกในการปรับแต่งเครื่องหมายพื้นฐาน
ใช้ขนาดวงกลม:
คุณวาดวงกลม (หรือรูปร่างอื่นๆ) ที่มีขนาดที่สัมพันธ์กับขนาดแผ่นดินไหวได้โดยใช้สัญลักษณ์ ในลักษณะนี้ แผ่นดินไหวที่รุนแรงจึงแสดงเป็นวงกลมที่ใหญ่ที่สุดบนแผนที่การใช้แผนที่ความหนาแน่น:
เลเยอร์แผนที่ความหนาแน่นในคลังภาพแสดงการกระจายของแผ่นดินไหวได้อย่างง่ายดายแต่ทรงพลัง แผนที่ความหนาแน่นใช้สีเพื่อแสดงความหนาแน่นของจุด ทำให้เลือกพื้นที่ที่มีกิจกรรมสูงได้ง่ายขึ้น นอกจากนี้แผนที่ความหนาแน่นยังใช้WeightedLocations
ได้ด้วย เพื่อให้ระบบแสดงแผ่นดินไหวที่ใหญ่ขึ้นได้ชัดเจนมากขึ้นในแผนที่ความหนาแน่น เป็นต้น
ขนาดวงกลม
แผนที่ด้านล่างแสดงเครื่องหมายที่กำหนดเองโดยใช้วงกลม วงกลมมีขนาดเพิ่มขึ้นตามขนาดของแผ่นดินไหว ณ ตำแหน่งนั้นๆ
ส่วนด้านล่างแสดงรหัสทั้งหมดที่คุณต้องสร้างแผนที่ด้วยเครื่องหมายวงกลมที่กำหนดเอง
TypeScript
let map: google.maps.Map; function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 2, center: { lat: -33.865427, lng: 151.196123 }, 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); map.data.setStyle((feature) => { const magnitude = feature.getProperty("mag"); return { icon: getCircle(magnitude), }; }); } function getCircle(magnitude: number) { return { path: google.maps.SymbolPath.CIRCLE, fillColor: "red", fillOpacity: 0.2, scale: Math.pow(2, magnitude) / 2, strokeColor: "white", strokeWeight: 0.5, }; } function eqfeed_callback(results: any) { map.data.addGeoJson(results); } 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: { lat: -33.865427, lng: 151.196123 }, 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); map.data.setStyle((feature) => { const magnitude = feature.getProperty("mag"); return { icon: getCircle(magnitude), }; }); } function getCircle(magnitude) { return { path: google.maps.SymbolPath.CIRCLE, fillColor: "red", fillOpacity: 0.2, scale: Math.pow(2, magnitude) / 2, strokeColor: "white", strokeWeight: 0.5, }; } function eqfeed_callback(results) { map.data.addGeoJson(results); } 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 Circles</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 callback 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>
ลองใช้ตัวอย่าง
แผนที่ความหนาแน่น
แผนที่ความหนาแน่นช่วยให้ผู้ชมเข้าใจการกระจายตัวของแผ่นดินไหวได้ง่ายขึ้น ซึ่งรายงานโดย USGS แทนที่จะวางเครื่องหมายบนจุดเหนือศูนย์แต่ละจุด แผนที่ความร้อนจะใช้สีและรูปร่างเพื่อแสดงการกระจายข้อมูล ในตัวอย่างนี้ สีแดงหมายถึงพื้นที่ที่เกิดเหตุการณ์แผ่นดินไหวสูง
ส่วนด้านล่างแสดงรหัสทั้งหมดที่คุณต้องสร้างแผนที่นี้
TypeScript
let map: google.maps.Map; function initMap(): void { map = new google.maps.Map(document.getElementById("map") as HTMLElement, { zoom: 2, center: { lat: -33.865427, lng: 151.196123 }, 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); } function eqfeed_callback(results: any) { const heatmapData: google.maps.LatLng[] = []; 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]); heatmapData.push(latLng); } const heatmap = new google.maps.visualization.HeatmapLayer({ data: heatmapData, dissipating: false, 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: { lat: -33.865427, lng: 151.196123 }, 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); } function eqfeed_callback(results) { const heatmapData = []; 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]); heatmapData.push(latLng); } const heatmap = new google.maps.visualization.HeatmapLayer({ data: heatmapData, dissipating: false, 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 Heatmap</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 callback 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&libraries=visualization&v=weekly" defer ></script> </body> </html>
ลองใช้ตัวอย่าง
ข้อมูลเพิ่มเติม
อ่านเพิ่มเติมเกี่ยวกับหัวข้อต่อไปนี้