Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use browser GeoLocation without Google API?

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)

enter image description here

now on the console, there isn't any data for navigator.geolocation:

enter image description here

like image 943
nima Avatar asked Jul 02 '20 06:07

nima


People also ask

How do I access geolocation in my browser?

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).

Is HTML Geolocation API free?

So yes, getting a user's lat/lon is 'free'--no need to pay for that feature by itself.

Do all browsers support geolocation?

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.

How do I add a geolocation to my website?

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.


2 Answers

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). enter image description here

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.

like image 120
Ali Torki Avatar answered Sep 17 '22 12:09

Ali Torki


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;

  1. each and every user geolocation without their permission
  2. 100% accuracy without native GPS on their web accessing device and when vpn, proxies are in-use.
like image 23
Dhanuka Jayasinghe Avatar answered Sep 21 '22 12:09

Dhanuka Jayasinghe