2

Firstly, I am aware that this post exists. I am in need of a free (or very cheap) solution so I'm asking this question again.

getCurrentPosition() and watchPosition() are deprecated on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS.

Chrome has disallowed the usage of getCurrentPosition() for websites without SSL.

Google Maps API could solve this but its usage limits is way too low. (We are managing a news portal with more than 400,000 page views per day).

Does anyone know any free (or very cheap) alternative to getCurrentPosition()?

halfer
  • 19,824
  • 17
  • 99
  • 186
Jacob Goh
  • 19,800
  • 5
  • 53
  • 73

1 Answers1

2

You could use the https://ipinfo.io API for this (it's my service). It's free for up to 1,000 req/day (with or without SSL support). It gives you coordinates, name and more. Here's an example:

curl ipinfo.io
{
  "ip": "172.56.39.47",
  "hostname": "No Hostname",
  "city": "Oakland",
  "region": "California",
  "country": "US",
  "loc": "37.7350,-122.2088",
  "org": "AS21928 T-Mobile USA, Inc.",
  "postal": "94621"
}

Here's an example which constructs a coords object with the API response that matches what you get from getCurrentPosition():

$.getJSON('https://ipinfo.io/geo', function(response) { 
    var loc = response.loc.split(',');
    var coords = {
        latitude: loc[0],
        longitude: loc[1]
    };
});

And here's a detailed example that shows how you can use it as a fallback for getCurrentPosition():

function do_something(coords) {
    // Do something with the coords here
}

navigator.geolocation.getCurrentPosition(function(position) { 
    do_something(position.coords);
    },
    function(failure) {
        $.getJSON('https://ipinfo.io/geo', function(response) { 
        var loc = response.loc.split(',');
        var coords = {
            latitude: loc[0],
            longitude: loc[1]
        };
        do_something(coords);
        });  
    };
});
Ben Dowling
  • 17,187
  • 8
  • 87
  • 103