I need to get the user's location from the Web client. Since Google restriction in some countries (for more information see this question on Stackoverflow), the below code will return an empty object (in the browser console, chrome, safari, etc.):
navigator.geolocation
So, I can't use getCurrentLocation
and other methods in geolocation API
I'm using different approaches to find user location (IP base) but it isn't working fine.
Is there another way to find user location coordinates?
UPDATED(July 12, 2020):
Due to misconceptions, I've updated my post. some peoples say it's work on HTTPS. so I opened the Chrome browser and search for StackOverflow (this site is well-known HTTPS)
now on the console, there isn't any data for navigator.geolocation:
The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).
So yes, getting a user's lat/lon is 'free'--no need to pay for that feature by itself.
Most modern web browsers support browser geolocation, but only in a secure context (HTTPS). Some older browsers may not support browser geolocation. It is advised to always check in your code whether browser geolocation is supported before attempting to use the functionality, as shown in this javascript example.
If supported, run the getCurrentPosition() method. If not, display a message to the user. If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition) The showPosition() function outputs the Latitude and Longitude.
MDN reference:
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
I think you are trying to use this feature in development mode which not works. But there is one way to solve this problem in development mode and that is using a fake https just in your development mode.
UPDATED(July 12, 2020):
My geolocation object when in the Stackoverflow console(inspect element).
Geolocation object is literally empty and you should see its prototypes user the __proto__
.
If you are using the create-react-app in your project, you should add HTTPS=true in your .env file. it worked for me.
To find geolocation of the users on a web client you can use HTML Geolocation API and Google Geolocation ( refer this question on stackoverflow ). But there is always need to be get the permission of users on allowing knowing their location (browser location permission) for on the browser. If users not allowed it, Geolocation api won't provide any result.
Eventhough users are allowed, some geolocations result from web browsers running on computers won't be precise or not even close (no built-in GPS), unlike on mobile devices (which use built-in GPS). Also finding precise geolocation will be problamatic if users are using a VPN or proxy.
So for a summary, we won't be able to get;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With