ওয়েব জিওলোকেশন এপিআই

ব্যবহারকারীর অবস্থান নির্ধারণ

HTML Geolocation API ব্যবহার করে ব্যবহারকারীর অবস্থান পাওয়া যায়。

এটা প্রাইভেসিতে ক্ষতি করতে পারে, তাই ব্যবহারকারীর অনুমোদন ছাড়াই স্থান ব্যবহার করা যায় না。

স্বয়ংক্রিয়ভাবে প্রয়াস করুন

মন্তব্য:জিপিএস সহ যন্ত্র (যেমন স্মার্টফোন) জ্যোতির্বিজ্ঞান সবচেয়ে নিশ্চিত

সমস্ত ব্রাউজার Geolocation API-এর সমর্থন করে

Chrome IE Firefox Safari Opera
সমর্থন সমর্থন সমর্থন সমর্থন সমর্থন

মন্তব্য:Chrome 50 থেকে, Geolocation API শুধুমাত্র নিরাপদ কনটেক্টে (যেমন HTTPS) প্রয়োগ হবে। আপনার সাইট HTTP (যেমন HTTP) হলে, ব্যবহারকারীর অবস্থান পাওয়ার অনুরোধ করা হবে না

Geolocation API-এর ব্যবহার

getCurrentPosition() এই পদ্ধতি ব্যবহারকারীর অবস্থান ফিরিয়ে দেয়

নিচের উদাহরণ ব্যবহারকারীর অবস্থানের দূরত্ব ও দূরত্ব ফিরিয়ে দেয়:

ইনস্ট্যান্স

<script>
const x = document.getElementById("demo");
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  }
    x.innerHTML = "এই ব্রাউজার গ্রোলোসিটি সমর্থিত না।";
  }
}
function showPosition(position) {
  x.innerHTML = "ল্যাটিটিউড: " + position.coords.latitude +
  "<br>লংগিটিউড: " + position.coords.longitude;
}
</script>

স্বয়ংক্রিয়ভাবে প্রয়াস করুন

উদাহরণ ব্যাখ্যা:

  1. Geolocation-এর সমর্থন করা হলে কি না পরীক্ষা করুন
  2. যদি সমর্থন করা হয়, তবে getCurrentPosition() পদ্ধতি চালানো হবে। যদি না, তবে ব্যবহারকারীকে একটি বার্তা দেখানো হবে
  3. যদি getCurrentPosition() পদ্ধতি সফল হয়, তবে এটি (showPosition) ফাংশনের প্রান্তদ্বারা একটি coordinates অবজেক্ট ফিরিয়ে দেয়
  4. showPosition() ফাংশন দূরত্ব ও দূরত্ব দেখায়

উপরোক্ত উদাহরণ একটি অত্যন্ত মৌলিক ভূগোল স্থানাঙ্কন স্ক্রিপ্ট, ত্রুটি হলে নেই।

ত্রুটি ও প্রত্যাখ্যান সমাধান

getCurrentPosition() মথদণ্ডের দ্বিতীয় প্রান্ত ত্রুটি হলে কীভাবে সমাধান করবে বলে নির্দেশ করে। যদি ব্যবহারকারীর অবস্থান পাওয়া যায় না, তবে এটি চালানো হবে ফাংশন:

ইনস্ট্যান্স

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation.";
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable.";
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out.";
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred.";
      break;
  }
}

স্বয়ংক্রিয়ভাবে প্রয়াস করুন

মানচিত্রে ফলাফল দেখান

মানচিত্রে ফলাফল দেখাতে, আপনাকে মানচিত্র সেবা সংগ্রহ করতে হবে, যেমন গুগল মানচিত্র。

In the following example, the returned latitude and longitude are used to display the location on Google Maps (using static images):

ইনস্ট্যান্স

function showPosition(position) {
  let latlon = position.coords.latitude + "," + position.coords.longitude;
  let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=
  "+latlon+"&zoom=14&size=400x300&sensor=false&key=YOUR_KEY";
  document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}

Location-specific information

This page demonstrates how to display the user's location on a map.

Geolocation is also very useful for location-specific information, such as:

  • Latest local information
  • Display points of interest near the user
  • Turn-by-turn navigation (GPS)

getCurrentPosition() method - Return data

getCurrentPosition() Method returns an object on success. It always returns latitude, longitude, and accuracy properties. If available, it returns other properties:

Properties Return
coords.latitude Latitude expressed as a decimal number (always returned).
coords.longitude Longitude expressed as a decimal number (always returned).
coords.accuracy Location accuracy (always returned).
coords.altitude Average height above sea level (in meters) (if available, returned).
coords.altitudeAccuracy Location height accuracy (if available, returned).
coords.heading Bearing from north clockwise (if available, returned).
coords.speed Speed in meters/second (if available, returned).
timestamp Response date/time (if available, returned).

Geolocation object - Other interesting methods

Geolocation object also has other interesting methods:

  • watchPosition() - Return the user's current location and continue to return updated locations as the user moves (such as GPS in a car).
  • clearWatch() - stop watchPosition () method.

নিচের উদাহরণ দেখুন watchPosition() পদ্ধতি।আপনাকে সঠিক জিপিএস ডিভাইস (যেমন স্মার্টফোন) ব্যবহার করে পরীক্ষা করতে হবে:

ইনস্ট্যান্স

<script>
const x = document.getElementById("demo");
function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.watchPosition(showPosition);
  }
    x.innerHTML = "এই ব্রাউজার গ্রোলোসিটি সমর্থিত না।";
  }
}
function showPosition(position) {
  x.innerHTML = "ল্যাটিটিউড: " + position.coords.latitude +
  "<br>লংগিটিউড: " + position.coords.longitude;
}
</script>

স্বয়ংক্রিয়ভাবে প্রয়াস করুন