【问题标题】:Geolocation Without SSL connection没有 SSL 连接的地理位置
【发布时间】:2017-01-14 23:01:14
【问题描述】:

我正在开发一个使用地理位置坐标的应用程序。 我使用 HTML5 地理定位功能来找出网站访问者的位置,但问题在于 Chrome 不支持不安全来源的 GeoLocation。

  function showPosition(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
            document.getElementById("result").innerHTML = positionInfo;
        });
    } else{
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
}

这里的代码 getCurrentPosition 无法使用,因为我的网站没有连接 SSL。

Chrome 警告: getCurrentPosition() 和 watchPosition() 不再适用于不安全的来源。要使用此功能,您应该考虑将您的应用程序切换到安全来源,例如 HTTPS。

还有其他方法可以在 chrome 上获取坐标/LatLong 值吗? [仅在不安全的连接上]

EDIT :它在我的机器上使用 localhost:80 工作,但在 http 上的测试 url 中不工作

【问题讨论】:

  • 谷歌搜索。但找不到好的解决方案。
  • 显而易见的问题:开始使用 HTTPS 不是一种选择……?
  • Https 是一个选项。但只是想知道是否有其他选择
  • 您可以使用 Google Maps Geolocation API。
  • 你可以看看gpsha.re

标签: html geolocation


【解决方案1】:
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();

【讨论】:

  • 此解决方案不返回准确的坐标
  • 此 Google API 上还有每日/每月/等使用配额。
  • 请在您的答案中添加一些解释,以便其他人可以从中学习。所有这些代码如何解决给定的问题?
  • 另外,不要假设每个人都使用 jQuery。真是一团糟。
【解决方案2】:

这是我在 2017/05 年针对 Safari 的非 SSL 解决方案

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();

新的部分是“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;

【讨论】:

    【解决方案3】:

    Chrome 现在可以防止不受信任的网站使用 html5 地理位置。受信任的站点包括 localhost 或任何 https 认证的域。在我的情况下,这些都不可能,所以我使用以下参数从命令行打开 chrome:open -a /Applications/Google\ Chrome.app' --args --unsafely-treat-insecure-origin-as-secure="http://yoursite.test" 将我的特定域添加为受信任的站点。

    【讨论】:

      【解决方案4】:

      非常简单:打开 Chrome,打开此地址并输入您希望启用的域

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

      这种方式是永久的,不需要每次都打开chrome浏览器

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

      如上所述。

      【讨论】:

      • 最好的方法!谢谢!
      • 这绝对是答案 ;)
      猜你喜欢
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2011-07-27
      相关资源
      最近更新 更多