+ 7

How do I get the location through the geolocation API in Html?

31st May 2017, 8:36 AM
Kabulput
Kabulput - avatar
9 Respuestas
+ 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
31st May 2017, 12:44 PM
Taija
Taija - avatar
+ 3
(...user location) this is correct answer
4th Jun 2020, 9:54 PM
Kareem Adel Awwad
Kareem Adel Awwad - avatar
+ 2
Thanks a lot 😄
31st May 2017, 12:53 PM
Kabulput
Kabulput - avatar
+ 1
(...user location) this is correct answer
28th Mar 2019, 10:25 AM
amit shaw
amit shaw - avatar
+ 1
User location
14th Nov 2020, 4:29 AM
JaKayla Eaddy
0
(...User location)is correct ans
22nd Jul 2019, 12:27 PM
prasad panchal
prasad panchal - avatar
0
civic + god:
9th Sep 2020, 8:34 AM
freelancer himayetali
freelancer himayetali - avatar
0
Which choice is the mandatory parameter of the getCurrentPosition() method?
14th Feb 2023, 4:11 AM
Sakiththiyan
Sakiththiyan - avatar
- 1
What is the Geolocation API? With the Geolocation API, you can obtain... Ans: ...user location
6th Jul 2022, 9:40 PM
Vijay Shivlal Vaghela
Vijay Shivlal Vaghela - avatar