खास जानकारी
किसी स्थानीय या रिमोट सोर्स से 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> <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()
तरीके का इस्तेमाल करें. फ़ाइल एक ही डोमेन पर होनी चाहिए,
लेकिन उसे किसी अलग सबडोमेन में होस्ट किया जा सकता है. उदाहरण के लिए, 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 का अनुरोध करने के लिए, अपने दस्तावेज़ में सबसे ऊपर script
टैग जोड़ने के लिए, createElement()
का इस्तेमाल करें.
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()
होता है. टारगेट डोमेन, कॉलबैक स्क्रिप्ट का नाम तय करता है. ब्राउज़र में टारगेट यूआरएल लोड करने पर, पेज पर दिखने वाला पहला नाम होता है.
उदाहरण के लिए, कॉलबैक नाम को eqfeed_callback
के तौर पर दिखाने के लिए, अपनी ब्राउज़र विंडो में http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
लोड करें.
आपको अपने कोड में कॉलबैक स्क्रिप्ट तय करनी होगी:
function eqfeed_callback(response) {
map.data.addGeoJson(response);
}
मैप पर पार्स किए गए GeoJSON डेटा को डालने के लिए, addGeoJson()
तरीके का इस्तेमाल करें.
डेटा का लुक तय करना
मैप ऑब्जेक्ट में GeoJSON डेटा जोड़कर, अपने डेटा को दिखाने का तरीका बदला जा सकता है. अपने डेटा को स्टाइल करने के बारे में ज़्यादा जानकारी के लिए, डेवलपर की गाइड पढ़ें.
ज़्यादा जानें
- GeoJSON एक ऐसा ओपन फ़ॉर्मैट है जो बड़े पैमाने पर भौगोलिक डेटा को कोड में बदलने के लिए इस्तेमाल किया जाता है. यह फ़ॉर्मैट, JSON (JavaScript ऑब्जेक्ट नोटेशन) पर आधारित होता है. JSON डेटा के लिए डिज़ाइन किए गए JavaScript टूल और तरीके, GeoJSON के साथ भी काम करते हैं. ज़्यादा जानकारी के लिए डेवलपर की गाइड पढ़ें.
- JSONP का मतलब है, पैडेड JSON. यह कम्यूनिकेशन का एक तरीका है, जिसका इस्तेमाल वेब ब्राउज़र में चलने वाले JavaScript प्रोग्राम में किया जाता है. इससे, किसी दूसरे डोमेन के सर्वर से डेटा का अनुरोध करने में मदद मिलती है.