15

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

nima
  • 7,796
  • 12
  • 36
  • 53
  • 1
    How much precision are you looking for in locating the user? – diogo.silva Jul 04 '20 at 12:00
  • @i.am.diogo.silva, Thanks for your good question. getting user's location IP isn't precise at all (user's my use VPN, proxy etc.) Unfortunately in this project, I need a precise location but is there any way at all? – nima Jul 05 '20 at 05:07
  • Geolocation in browser will work with googleapi, due to google restriction we can't access web location (chrome, safari, mozila, IE, opera ...) in anywhere like localhost @Towerss – nima Jul 05 '20 at 05:53

5 Answers5

4

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.

Ali Torki
  • 1,929
  • 16
  • 26
1

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

An alternative choice for an API could be GeoJS API. It is based on Maxmind's GeoLite database.

Notice: in the above mentioned API all HTTP requests will redirect to HTTPS.

-1

You could try to get the users location using the geolocation API, and have a fallback that uses the user's IP address in case the API does not work in the user's environment.

const success = (position) => {
  // use position
};

const fallback = () => {
  // try getting it via the user's IP
}

navigator.geolocation.getCurrentPosition(success, fallback);

Of course, it all depends on how much precision you need. IP address localization does not have a lot of precision.

diogo.silva
  • 448
  • 4
  • 13
  • Yes, this is a usual way to check geolocation fallback. I'm using this approach in my project but the question is about using geolocation without google API. Thanks for your answer. – nima Jul 05 '20 at 05:01
  • Keep in mind that the Geolocation API is not a Google API: https://caniuse.com/#feat=geolocation – diogo.silva Jul 05 '20 at 14:17
  • thanks for your response, but I've updated my post. please check it again. – nima Jul 12 '20 at 10:09
-2

If you would like to find the user location from the client-side without using the Google geolocation API, you still can use the browser embedded geolocation API which is widely supported in many browsers. The main difference between the two is that the browser embedded API will ask for the user's permission to share their location, whilst the Google one skips that step. Also, the native browser geolocation API works over https connections or in a secure context.

If you already have the user's IP address, you could make a call to a geolocation service. A good free service is IPSTACK, and here is an example from their documentation using JQuery:

// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';

// get the API result via jQuery.ajax
$.ajax({
    url: 'https://api.ipstack.com/' + ip + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // output the "capital" object inside "location"
        alert(json.location.capital);
        
    }
});
Towerss
  • 629
  • 2
  • 12
  • 26
  • 2
    I was wondering what the difference between those two were - great to know! – will-yama Jul 05 '20 at 06:30
  • As I mentioned in my question, I'm not looking for an IP based solution. Thank's for your response. Google Geolocation and browser embedded geolocation API not working as a write-in question. @will-yama – nima Jul 05 '20 at 08:22