[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/API/Geolocation_API/Using_the_Geolocation_API [Back]  [Original]

Geolocation API - API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Geolocation API

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Geolocation API web-, . , .

In this article

API navigator.geolocation.

, . :

js
if ("geolocation" in navigator) {
  /*   */
} else {
  /*    */
}

, getCurrentPosition(). , , . , callback. callback , . , - , , , , , .

: getCurrentPosition() , , . , , . GPS, , GPS , ( IP wifi-), getCurrentPosition().

js
navigator.geolocation.getCurrentPosition(function (position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

do_something(), , , .

( , ), callback , . watchPosition(), : getCurrentPosition(). , , ( ). , , getCurrentPosition(), , .

js
var watchID = navigator.geolocation.watchPosition(function (position) {
  do_something(position.coords.latitude, position.coords.longitude);
});

watchPosition() ID, ; clearWatch(), .

js
navigator.geolocation.clearWatch(watchID);

getCurrentPosition() watchPosition() - , - PositionOptions.

, ( , , ; ), , , .

watchPosition :

js
function geo_success(position) {
  do_something(position.coords.latitude, position.coords.longitude);
}

function geo_error() {
  alert(",   .");
}

var geo_options = {
  enableHighAccuracy: true,
  maximumAge: 30000,
  timeout: 27000,
};

var wpid = navigator.geolocation.watchPosition(
  geo_success,
  geo_error,
  geo_options,
);

GeolocationPosition, GeolocationCoordinates.

GeolocationPosition , coords, GeolocationCoordinates timestamp, DOMTimeStamp, , .

GeolocationCoordinates , : latitude longitude, . - :

js
function success(position) {
  const latitude = position.coords.latitude;
  const longitude = position.coords.longitude;

  //  ,  -   (latitude)  (longitude)
}

, GeolocationCoordinates, , , , .

Callback- , getCurrentPosition() watchPosition(), GeolocationPositionError . , code, , message, code.

:

js
function errorCallback(error) {
  alert("ERROR(" + error.code + "): " + error.message);
}

Geolocation API , . , openstreetmap.org, .

HTML

html
<button id="find-me">Show my location</button><br />
<p id="status"></p>
<a id="map-link" target="_blank"></a>

JavaScript

js
function geoFindMe() {
  const status = document.querySelector("#status");
  const mapLink = document.querySelector("#map-link");

  mapLink.href = "";
  mapLink.textContent = "";

  function success(position) {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;

    status.textContent = "";
    mapLink.href = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
    mapLink.textContent = `: ${latitude} , : ${longitude} `;
  }

  function error() {
    status.textContent = "   ";
  }

  if (!navigator.geolocation) {
    status.textContent = "Geolocation    ";
  } else {
    status.textContent = " ";
    navigator.geolocation.getCurrentPosition(success, error);
  }
}

document.querySelector("#find-me").addEventListener("click", geoFindMe);


Web Proxy Viewer  |  New URL  |  Original Page