| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/API/Geolocation_API/Using_the_Geolocation_API | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Geolocation API web-, . , .
, . :
if ("geolocation" in navigator) {
/* */
} else {
/* */
}
, getCurrentPosition(). , , . , callback. callback , . , - , , , , , .
:
getCurrentPosition() , , . , , . GPS, , GPS , ( IP wifi-), getCurrentPosition().
navigator.geolocation.getCurrentPosition(function (position) {
do_something(position.coords.latitude, position.coords.longitude);
});
do_something(), , , .
( , ), callback , . watchPosition(), : getCurrentPosition(). , , ( ). , , getCurrentPosition(), , .
var watchID = navigator.geolocation.watchPosition(function (position) {
do_something(position.coords.latitude, position.coords.longitude);
});
watchPosition() ID, ; clearWatch(), .
navigator.geolocation.clearWatch(watchID);
getCurrentPosition() watchPosition() - , - PositionOptions.
, ( , , ; ), , , .
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, . - :
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// , - (latitude) (longitude)
}
, GeolocationCoordinates, , , , .
Callback- , getCurrentPosition() watchPosition(), GeolocationPositionError . , code, , message, code.
:
function errorCallback(error) {
alert("ERROR(" + error.code + "): " + error.message);
}
Geolocation API , . , openstreetmap.org, .
body {
padding: 20px;
background-color: #ffffc9;
}
button {
margin: 0.5rem 0;
}
<button id="find-me">Show my location</button><br />
<p id="status"></p>
<a id="map-link" target="_blank"></a>
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);
This page was last modified on 17 . 2024 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |