Geolocation Without SSL connection

前端 未结 4 1810
耶瑟儿~
耶瑟儿~ 2020-12-13 20:50

I am developing an application which uses geolocation coordinates. I used the HTML5 geolocation feature to find out the location of the site visitor, but the issue is with C

相关标签:
4条回答
  • 2020-12-13 21:07

    Here is my non SSL solution for Safari in 2017/05

    var apiGeolocationSuccess = function(position) {
        alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
    };
    
    var tryAPIGeolocation = function() {
        jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
            apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
        })
            .fail(function(err) {
                alert("API Geolocation error! \n\n"+err);
            });
    };
    
    var browserGeolocationSuccess = function(position) {
        alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
    };
    
    var browserGeolocationFail = function(error) {
        switch (error.code) {
            case error.TIMEOUT:
                alert("Browser geolocation error !\n\nTimeout.");
                break;
            case error.PERMISSION_DENIED:
                if(error.message.indexOf("Only secure origins are allowed") == 0) {
                    tryAPIGeolocation();
                }
                break;
            case error.POSITION_UNAVAILABLE:
                // dirty hack for safari
                if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
                    tryAPIGeolocation();
                } else {
                    alert("Browser geolocation error !\n\nPosition unavailable.");
                }
                break;
        }
    };
    
    var tryGeolocation = function() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                browserGeolocationSuccess,
                browserGeolocationFail,
                {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
        }
    };
    
    tryGeolocation();
    

    The new part is this one in "case error.POSITION_UNAVAILABLE":

    case error.POSITION_UNAVAILABLE:
    // dirty hack for safari
    if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
        tryAPIGeolocation();
    } else {
        alert("Browser geolocation error !\n\nPosition unavailable.");
    }
    break;
    
    0 讨论(0)
  • 2020-12-13 21:11

    Really simple: open Chrome, open this address and enter the domains you wish to enable for

    chrome://flags/#unsafely-treat-insecure-origin-as-secure
    

    This way it's permanent and you don't need to open the chrome browser each time with

    google-chrome  --args --unsafely-treat-insecure-origin-as-secure="http://whatever.test"`
    

    as answered above.

    0 讨论(0)
  • 2020-12-13 21:19

    Chrome now prevents untrusted sites from using html5 geolocation. Trusted sites include localhostor any https certified domain. In my case neither of these were possible so instead i opened chrome from the command line with this argument: open -a /Applications/Google\ Chrome.app' --args --unsafely-treat-insecure-origin-as-secure="http://yoursite.test" Which added my particular domain as a trusted site.

    0 讨论(0)
  • 2020-12-13 21:23
    var apiGeolocationSuccess = function(position) {
        alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
    };
    
    var tryAPIGeolocation = function() {
        jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
            apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
      })
      .fail(function(err) {
        alert("API Geolocation error! \n\n"+err);
      });
    };
    
    var browserGeolocationSuccess = function(position) {
        alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
    };
    
    var browserGeolocationFail = function(error) {
      switch (error.code) {
        case error.TIMEOUT:
          alert("Browser geolocation error !\n\nTimeout.");
          break;
        case error.PERMISSION_DENIED:
          if(error.message.indexOf("Only secure origins are allowed") == 0) {
            tryAPIGeolocation();
          }
          break;
        case error.POSITION_UNAVAILABLE:
          alert("Browser geolocation error !\n\nPosition unavailable.");
          break;
      }
    };
    
    var tryGeolocation = function() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            browserGeolocationSuccess,
          browserGeolocationFail,
          {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
      }
    };
    
    tryGeolocation();
    
    0 讨论(0)
提交回复
热议问题