Maps में GeoJSON डेटा इंपोर्ट करना

खास जानकारी

स्थानीय या रिमोट सोर्स से 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;

सीएसएस

/* 
 * 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>
  <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>

सैंपल आज़माएं

डेटा लोड हो रहा है

इस सेक्शन में बताया गया है कि Maps JavaScript API ऐप्लिकेशन के डोमेन से या किसी दूसरे डोमेन से डेटा कैसे लोड करें.

एक ही डोमेन से डेटा लोड करना

Google Maps डेटा लेयर, किसी भी भू-स्थानिक डेटा (इसमें GeoJSON भी शामिल है) के लिए कंटेनर उपलब्ध कराती है. अगर आपका डेटा, Maps JavaScript API ऐप्लिकेशन के तौर पर उसी डोमेन पर होस्ट की गई फ़ाइल में है, तो उसे map.data.loadGeoJson() तरीके का इस्तेमाल करके लोड किया जा सकता है. फ़ाइल एक ही डोमेन पर होनी चाहिए. हालांकि, इसे किसी दूसरे सबडोमेन में होस्ट किया जा सकता है. उदाहरण के लिए, www.example.com से files.example.com पर अनुरोध किया जा सकता है.

map.data.loadGeoJson('data.json');

अलग-अलग डोमेन का डेटा लोड हो रहा है

अगर डोमेन का कॉन्फ़िगरेशन इस तरह के अनुरोध की अनुमति देता है, तो अपने डोमेन के अलावा किसी दूसरे डोमेन से भी डेटा का अनुरोध किया जा सकता है. इस अनुमति के लिए स्टैंडर्ड को क्रॉस-ऑरिजिन रिसॉर्स शेयरिंग (सीओआरएस) कहा जाता है. अगर किसी डोमेन ने क्रॉस-डोमेन अनुरोधों को अनुमति दी है, तो उसके जवाब के हेडर में यह एलान शामिल होना चाहिए:

Access-Control-Allow-Origin: *

यह पता लगाने के लिए कि किसी डोमेन ने सीओआरएस चालू किया है या नहीं, Chrome डेवलपर टूल (DevTools) का इस्तेमाल करें.

ऐसे डोमेन से डेटा लोड करना, उसी डोमेन से 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() होता है. टारगेट डोमेन, कॉलबैक स्क्रिप्ट का नाम तय करता है. यह नाम, ब्राउज़र में टारगेट यूआरएल लोड करने पर पेज पर दिखता है.

उदाहरण के लिए, अपने ब्राउज़र विंडो में http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp लोड करें. इससे आपको कॉलबैक का नाम eqfeed_callback दिखेगा.

आपको अपने कोड में कॉलबैक स्क्रिप्ट तय करनी होगी:

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

पार्स किए गए GeoJSON डेटा को मैप पर रखने के लिए, addGeoJson() तरीके का इस्तेमाल करें.

डेटा को स्टाइल करना

Map ऑब्जेक्ट में GeoJSON डेटा जोड़कर, अपने डेटा के दिखने के तरीके को बदला जा सकता है. अपने डेटा को स्टाइल करने के बारे में ज़्यादा जानने के लिए, डेवलपर गाइड पढ़ें.

ज़्यादा जानें

  • GeoJSON, भौगोलिक डेटा को कोड में बदलने के लिए इस्तेमाल किया जाने वाला एक ओपन फ़ॉर्मैट है. यह JSON (JavaScript Object Notation) पर आधारित है. JSON डेटा के लिए डिज़ाइन किए गए JavaScript टूल और तरीके, GeoJSON के साथ भी काम करते हैं. ज़्यादा जानकारी के लिए, डेवलपर गाइड पढ़ें.
  • JSONP का मतलब पैडेड JSON होता है. यह एक कम्यूनिकेशन मेथड है. इसका इस्तेमाल JavaScript प्रोग्राम में किया जाता है. ये प्रोग्राम, वेब ब्राउज़र में चलते हैं. इनकी मदद से, किसी दूसरे डोमेन के सर्वर से डेटा का अनुरोध किया जाता है.