মানচিত্রে জিওজেএসএন ডেটা আমদানি করুন

সংক্ষিপ্ত বিবরণ

স্থানীয় বা দূরবর্তী উৎস থেকে কীভাবে GeoJSON ডেটা ইম্পোর্ট করতে হয় এবং আপনার ম্যাপে তা প্রদর্শন করতে হয়, তা শিখুন। এই টিউটোরিয়ালটিতে ম্যাপে ডেটা ইম্পোর্ট করার বিভিন্ন কৌশল বোঝানোর জন্য নিচের ম্যাপটি ব্যবহার করা হয়েছে।

এই টিউটোরিয়ালে ম্যাপ তৈরি করার জন্য প্রয়োজনীয় সম্পূর্ণ কোডটি নিচের অংশে দেখানো হয়েছে।

টাইপস্ক্রিপ্ট

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;

জাভাস্ক্রিপ্ট

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 অ্যাপ্লিকেশনের একই ডোমেইন থেকে অথবা ভিন্ন কোনো ডোমেইন থেকে ডেটা লোড করতে হয়।

একই ডোমেইন থেকে ডেটা লোড করা হচ্ছে

গুগল ম্যাপস ডেটা লেয়ার যেকোনো ভূ-স্থানিক ডেটার (GeoJSON সহ) জন্য একটি ধারক প্রদান করে। যদি আপনার ডেটা আপনার ম্যাপস জাভাস্ক্রিপ্ট এপিআই অ্যাপ্লিকেশনের মতো একই ডোমেইনে হোস্ট করা কোনো ফাইলে থাকে, তাহলে আপনি map.data.loadGeoJson() মেথড ব্যবহার করে তা লোড করতে পারেন। ফাইলটি অবশ্যই একই ডোমেইনে থাকতে হবে, তবে আপনি এটিকে একটি ভিন্ন সাবডোমেইনে হোস্ট করতে পারেন। উদাহরণস্বরূপ, আপনি www.example.com থেকে files.example.com- এ একটি অনুরোধ পাঠাতে পারেন।

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

ডোমেইন জুড়ে ডেটা লোড করা

আপনি আপনার নিজের ডোমেইন ছাড়াও অন্য কোনো ডোমেইন থেকে ডেটার জন্য অনুরোধ করতে পারেন, যদি সেই ডোমেইনের কনফিগারেশন এই ধরনের অনুরোধের অনুমতি দেয়। এই অনুমতির জন্য ব্যবহৃত স্ট্যান্ডার্ডকে ক্রস-অরিজিন রিসোর্স শেয়ারিং (CORS) বলা হয়। যদি কোনো ডোমেইন ক্রস-ডোমেইন অনুরোধের অনুমতি দিয়ে থাকে, তবে তার রেসপন্স হেডারে নিম্নলিখিত ডিক্লারেশনটি অন্তর্ভুক্ত থাকা উচিত:

Access-Control-Allow-Origin: *

কোনো ডোমেইনে CORS সক্রিয় করা আছে কিনা তা জানতে Chrome Developer Tools (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() মেথডটি ব্যবহার করুন।

ডেটা স্টাইল করা

একটি Map অবজেক্টে GeoJSON ডেটা যোগ করে আপনি আপনার ডেটার চেহারা পরিবর্তন করতে পারেন। আপনার ডেটার স্টাইলিং সম্পর্কে আরও তথ্যের জন্য ডেভেলপার গাইডটি পড়ুন।

আরও জানুন

  • জিওজেসন (GeoJSON) হলো ভৌগোলিক ডেটা এনকোড করার জন্য বহুল ব্যবহৃত একটি ওপেন ফরম্যাট, যা জেএসওএন (JSON) (জাভাস্ক্রিপ্ট অবজেক্ট নোটেশন)-এর উপর ভিত্তি করে তৈরি। জেএসওএন ডেটার জন্য ডিজাইন করা জাভাস্ক্রিপ্ট টুল এবং মেথডগুলো জিওজেসন-এর সাথেও কাজ করে। আরও তথ্যের জন্য ডেভেলপারস গাইড পড়ুন।
  • JSONP-এর পূর্ণরূপ হলো প্যাডেড JSON। এটি ওয়েব ব্রাউজারে চালিত জাভাস্ক্রিপ্ট প্রোগ্রামে ব্যবহৃত একটি যোগাযোগ পদ্ধতি, যা ভিন্ন ডোমেইনের কোনো সার্ভার থেকে ডেটা অনুরোধ করার জন্য ব্যবহৃত হয়।