+ 7
How do I get the location through the geolocation API in Html?
9 Respostas
+ 4
You can get it by using the Navigator object's geolocation property, which is fetched from a window object's property. Geolocation has a method called getCurrentPosition and if its successful, you can use the location value however you want in a callback function.
Here's the basic idea:
''''''''''''''''''''''''''
navigator = window.navigator;
if (navigator.geolocation) {
// Get the user's current position and pass it to successFunc
// Th method takes options as well. but that's not important right now
navigator.geolocation.getCurrentPosition(successFunc, error, options);
} else {
// If geolocation doesn't work, leave an error message
alert('Geolocation is not supported or turned on in your browser');
}
// Position has cordinates as well as a timestamp as properties
function successFunc(position) {
latitude, longitude = position.coords.latitude, position.coords.longitude;
timestamp = position.timestamp;
}
''''''''''''''''''''
You can, for example, use the cordinates to find out the city or create a map based on the location (there are many open source APIs that let you do that). There's also another method called watchPosition that can fetch the data at regular intervals, following the location.
Here's a good article to get you started: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation
+ 3
(...user location) this is correct answer
+ 2
Thanks a lot 😄
+ 1
(...user location) this is correct answer
+ 1
User location
0
(...User location)is correct ans
0
civic + god:
0
Which choice is the mandatory parameter of the getCurrentPosition() method?
- 1
What is the Geolocation API?
With the Geolocation API, you can obtain...
Ans:
...user location