การนำเข้าข้อมูลไปยัง Maps

ภาพรวม

ดูวิธีนำเข้าข้อมูล 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 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>

ลองใช้ตัวอย่าง

กำลังโหลดข้อมูล

ส่วนนี้จะแสดงวิธีโหลดข้อมูลจากโดเมนเดียวกับแอปพลิเคชัน 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() โดเมนเป้าหมายจะกำหนดชื่อสคริปต์การเรียกกลับ ซึ่งเป็นชื่อแรกบนหน้าเว็บเมื่อคุณโหลด URL เป้าหมายในเบราว์เซอร์

ตัวอย่างเช่น โหลด http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp ในหน้าต่างเบราว์เซอร์เพื่อแสดงชื่อเรียกกลับเป็น eqfeed_callback

คุณต้องกำหนดสคริปต์เรียกกลับในโค้ดของคุณดังนี้

function eqfeed_callback(response) {
  map.data.addGeoJson(response);
}

ใช้วิธีการ addGeoJson() เพื่อวางข้อมูล GeoJSON ที่แยกวิเคราะห์แล้วลงในแผนที่

การจัดรูปแบบข้อมูล

คุณเปลี่ยนลักษณะของข้อมูลได้โดยเพิ่มข้อมูล GeoJSON ลงในออบเจ็กต์แผนที่ อ่านคู่มือนักพัฒนาซอฟต์แวร์เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับการจัดรูปแบบข้อมูล

ดูข้อมูลเพิ่มเติม

  • GeoJSON เป็นรูปแบบเปิดที่ใช้กันอย่างแพร่หลายสำหรับการเข้ารหัสข้อมูลทางภูมิศาสตร์ โดยอิงตาม JSON (JavaScript Object Notation) เครื่องมือและเมธอด JavaScript ที่ออกแบบมา สำหรับข้อมูล JSON ยังใช้ร่วมกับ GeoJSON ได้ด้วย อ่านข้อมูลเพิ่มเติมได้ในคู่มือสำหรับนักพัฒนาซอฟต์แวร์
  • JSONP ย่อมาจาก JSON padded เป็นวิธีการสื่อสารที่ใช้ในโปรแกรม JavaScript ที่ทำงานในเว็บเบราว์เซอร์เพื่อขอข้อมูลจากเซิร์ฟเวอร์ในโดเมนอื่น