ภาพรวม
บทแนะนำนี้จะแสดงวิธีแสดงข้อมูลเป็นภาพบน Google Maps ตัวอย่างเช่น แผนที่ในบทแนะนำนี้แสดงภาพข้อมูลเกี่ยวกับตำแหน่งของแผ่นดินไหวและความรุนแรง เรียนรู้เทคนิคที่จะใช้กับแหล่งข้อมูลของคุณเอง และสร้างเรื่องราวที่น่าสนใจบน Google Maps เช่น เรื่องราวที่อยู่ด้านล่าง
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> <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>
ลองใช้ตัวอย่าง
ใช้รูปร่างและฮีตแมปเพื่อปรับแต่งแผนที่
ส่วนนี้จะแสดงวิธีอื่นๆ ในการปรับแต่งชุดข้อมูลที่สมบูรณ์บนแผนที่ พิจารณาแผนที่ที่สร้างขึ้นในส่วนก่อนหน้าของบทแนะนำนี้ ซึ่ง แสดงเครื่องหมายในตำแหน่งแผ่นดินไหวทุกแห่ง คุณปรับแต่งเครื่องหมายเพื่อแสดงข้อมูลเพิ่มเติมได้ เช่น สถานที่ที่เกิดแผ่นดินไหวมากที่สุด รวมถึงขนาดหรือความลึก
ตัวเลือกบางอย่างในการปรับแต่งเครื่องหมายพื้นฐานมีดังนี้
การใช้ขนาดวงกลม
คุณวาดวงกลม (หรือรูปร่างอื่นๆ) ที่มีขนาดสัมพันธ์กับ ความรุนแรงของแผ่นดินไหวได้โดยใช้สัญลักษณ์ ด้วยวิธีนี้ แผ่นดินไหวที่รุนแรงจะแสดงเป็นวงกลมที่ใหญ่ที่สุดบนแผนที่การใช้แผนที่ความหนาแน่น
เลเยอร์แผนที่ความหนาแน่นในไลบรารีการแสดงภาพเป็นวิธีที่เรียบง่ายแต่ทรงพลัง ในการแสดงการกระจายตัวของแผ่นดินไหว ฮีตแมปใช้สีเพื่อ แสดงความหนาแน่นของจุด ทำให้เลือกพื้นที่ที่มีกิจกรรมสูงได้ง่ายขึ้น ฮีตแมปยังใช้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") as number; 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> <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>
ลองใช้ตัวอย่าง
แผนที่ความหนาแน่น
แผนที่ความหนาแน่นช่วยให้ผู้ชมเข้าใจการกระจายตัวของแผ่นดินไหวที่ 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> <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&libraries=visualization&v=weekly" defer ></script> </body> </html>
ลองใช้ตัวอย่าง
ข้อมูลเพิ่มเติม
อ่านเพิ่มเติมเกี่ยวกับหัวข้อต่อไปนี้