जियोलोकेशन: Maps पर उपयोगकर्ता या डिवाइस की स्थिति दिखाना

खास जानकारी

इस ट्यूटोरियल में, Google Maps पर किसी डिवाइस की जगह की जानकारी दिखाने का तरीका बताया गया है. इसके लिए, आपके ब्राउज़र की HTML5 भौगोलिक-स्थान सुविधा और Maps JavaScript API के साथ. कॉन्टेंट बनाने भौगोलिक जगह सिर्फ़ तब दिखेगी, जब उपयोगकर्ता ने जगह की जानकारी शेयर करने की अनुमति दी हो.

जब उपयोगकर्ता भौगोलिक स्थान का अनुरोध ट्रिगर करता है, तो उसे ब्राउज़र से एक अनुरोध मिलेगा डिवाइस की जगह की जानकारी का डेटा ऐक्सेस करने की सहमति पाने के लिए. अगर अनुरोध पूरा नहीं होता है, तो इसकी ये वजहें हो सकती हैं जगह की जानकारी की अनुमतियां अस्वीकार कर दी गई हैं या डिवाइस इसकी जगह का पता नहीं लगा सका. यह सुविधा सिर्फ़ सुरक्षित कॉन्टेक्स्ट (एचटीटीपीएस) में उपलब्ध है. यह कुछ या इसके साथ काम करने वाले सभी ब्राउज़र में उपलब्ध है.

नीचे एक मैप दिया गया है, जिससे उपयोगकर्ता के डिवाइस की मौजूदा जगह की पहचान की जा सकती है.

नीचे दिया गया नमूना वह पूरा कोड दिखाता है, जिसकी ज़रूरत आपको यह मैप बनाने के लिए होती है.

TypeScript

// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
let map: google.maps.Map, infoWindow: google.maps.InfoWindow;

function initMap(): void {
  map = new google.maps.Map(document.getElementById("map") as HTMLElement, {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 6,
  });
  infoWindow = new google.maps.InfoWindow();

  const locationButton = document.createElement("button");

  locationButton.textContent = "Pan to Current Location";
  locationButton.classList.add("custom-map-control-button");

  map.controls[google.maps.ControlPosition.TOP_CENTER].push(locationButton);

  locationButton.addEventListener("click", () => {
    // Try HTML5 geolocation.
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position: GeolocationPosition) => {
          const pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
          };

          infoWindow.setPosition(pos);
          infoWindow.setContent("Location found.");
          infoWindow.open(map);
          map.setCenter(pos);
        },
        () => {
          handleLocationError(true, infoWindow, map.getCenter()!);
        }
      );
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter()!);
    }
  });
}

function handleLocationError(
  browserHasGeolocation: boolean,
  infoWindow: google.maps.InfoWindow,
  pos: google.maps.LatLng
) {
  infoWindow.setPosition(pos);
  infoWindow.setContent(
    browserHasGeolocation
      ? "Error: The Geolocation service failed."
      : "Error: Your browser doesn't support geolocation."
  );
  infoWindow.open(map);
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

JavaScript

// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not give permission for the browser to
// locate you.
let map, infoWindow;

function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 6,
  });
  infoWindow = new google.maps.InfoWindow();

  const locationButton = document.createElement("button");

  locationButton.textContent = "Pan to Current Location";
  locationButton.classList.add("custom-map-control-button");
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(locationButton);
  locationButton.addEventListener("click", () => {
    // Try HTML5 geolocation.
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        (position) => {
          const pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
          };

          infoWindow.setPosition(pos);
          infoWindow.setContent("Location found.");
          infoWindow.open(map);
          map.setCenter(pos);
        },
        () => {
          handleLocationError(true, infoWindow, map.getCenter());
        },
      );
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
  });
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
  infoWindow.setPosition(pos);
  infoWindow.setContent(
    browserHasGeolocation
      ? "Error: The Geolocation service failed."
      : "Error: Your browser doesn't support geolocation.",
  );
  infoWindow.open(map);
}

window.initMap = initMap;
उदाहरण देखें

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

भौगोलिक-स्थान क्या है?

जियोलोकेशन का मतलब, किसी कंप्यूटिंग डिवाइस के भौगोलिक स्थान की पहचान से है डेटा इकट्ठा करने के कई तरीके इस्तेमाल किए जा सकते हैं. आम तौर पर, भौगोलिक स्थान वाली ज़्यादातर सेवाएं नेटवर्क का इस्तेमाल करती हैं इस जगह का पता लगाने के लिए पते या इंटरनल जीपीएस चिप को रूट कर सकता है. भौगोलिक-स्थान एक खास तौर पर किसी डिवाइस के लिए बना एपीआई. इसका मतलब है कि जिन ब्राउज़र या डिवाइसों में जियोलोकेशन की सुविधा काम करती है उन्हें Google Ads API का इस्तेमाल करने के लिए उसे वेब ऐप्लिकेशन से खोजते हैं.

W3C जियोलोकेशन स्टैंडर्ड

जो एप्लिकेशन भौगोलिक स्थान निष्पादित करना चाहते हैं उन्हें W3C जियोलोकेशन स्टैंडर्ड. ध्यान दें कि ऊपर दिया गया सैंपल कोड, W3C की मदद से डिवाइस की जगह की जानकारी तय करता है navigator.geolocation एपीआई.

कभी-कभी वेबसाइटें किसी डिवाइस की जगह का पता लगाने के लिए आईपी पतों का इस्तेमाल करती हैं. हालांकि, इससे सिर्फ़ उस जगह का मोटा अनुमान. W3C-स्टैंडर्ड एपीआई सबसे सही और पूरी तरह से काम करने वाले एपीआई हैं. इसलिए, उन्हें ऐसा होना चाहिए अन्य भौगोलिक स्थान विधियों पर प्राथमिकता दी जाती है.