0

I am trying to do a simple geolocation html5 :

$(function() {  
    var currentLatitude ="";
    var currentLongitude ="";
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    function showPosition(position) {
            currentLatitude = position.coords.latitude;
            currentLongitude = position.coords.longitude;                                                
        }   
    } else {
       // location based on the IP - less accurate

    }  

And the error is permission for location error even when i had my site(running local) on exceptions . My second option is by IP (ipinfo.io) :

function showError(error) {
      var x = $('#curr_loc_target');

      switch(error.code) {
        case error.PERMISSION_DENIED:
          x.html("User denied the request for Geolocation.");
           jQuery.get("http://ipinfo.io/json", function (response)
               {
                    console.log(response) ;
                   currentLatitude = response.loc.split(',')[0]; 
                   currentLongitude = response.loc.split(',')[1];
                   console.log("lat" + currentLatitude+ "," + "lngs" +currentLongitude) ;
               } );
          break;
        case error.POSITION_UNAVAILABLE:
          x.html("Location information is unavailable.");
          break;
        case error.TIMEOUT:
          x.html("The request to get user location timed out.");
          break;
        case error.UNKNOWN_ERROR:
          x.html("An unknown error occurred.");
          break;
        }
    }

And it works but it is not accurate at all . All i need to do is send lat , lng to my server , if it will be not local it will work ? or anyone knows a way to solve this error ?

1
  • I don't really know what kind of accuracy ipinfo has but it's clear that IP based geolocation has several drawbacks: it won't work on a local IP, if the user is behind a proxy, it's gonna return a wrong location and it's not always very accurate. I edited a service: userinfo.io which gives you the geolocation (and other information) from an IP. My tests so far show that it's pretty accurate but it has the same drawbacks I mentioned. I rely on several geoip providers so maybe it's gonna be a little more accurate but I can't be sure. Oct 2, 2014 at 16:39

2 Answers 2

0

The code working on my site. It seems like you have disabled the geolocation tracking on your browser that send you directly to the error scenario. Please take note that it will require user to explicitly grant you the permission to get the location tracking info.

0

I've tried to test geolocation, opening the .html file from my local machine, too - and Chrome always gave me the error callback here:

navigator.geolocation.getCurrentPosition(success, error);

inspite of the geolocation tracking was enabled. Other browsers gave the success callback. Then I put the page to the hosting, and it returned success in all browsers, including Google Chrome. May be you had similar reason with the permission for location when your site is running local.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.