OGeek|极客世界-中国程序员成长平台

标题: javascript - iOS Safari上javascript中的地理定位 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:36
标题: javascript - iOS Safari上javascript中的地理定位

我的目标是在 iOS 设备上使用 Safari 返回我的地理位置坐标。在我的桌面浏览器上,我使用 Google Maps Geolocation API key 调用下面的函数 tryGeolocation()

在桌面 Chrome 上有效,但在 Safari(桌面和 iOS)和 Chrome (iOS) 中,我返回 "Position Unavailable" 错误。我尝试过 5 秒和 10 秒的超时,但同样的情况发生了。

是否有在 iOS 中调用 Google Maps Geolocation API 的已知解决方法?

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="MY API KEY", 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();

我是使用 iOS 浏览器的新手,所以任何建议都将不胜感激!



Best Answer-推荐答案


这里是 Permissions 的示例和 Navigation涉及的 API

请记住,它仅当主机使用 HTTPS 时才有效。 这可能是由于您对以下答案的评论而导致权限警报未显示的原因。

请记住,如果您的主机不使用 HTTPS,它将超时并出现有关身份验证失败的错误。使用错误回调来检查。

if ( navigator.permissions && navigator.permissions.query) {
  //try permissions APIs first
  navigator.permissions.query({ name: 'geolocation' }).then(function(result) {
      // Will return ['granted', 'prompt', 'denied']
      const permission = result.state;
      if ( permission === 'granted' || permission === 'prompt' ) {
          _onGetCurrentLocation();
      }
  });
} else if (navigator.geolocation) {
  //then Navigation APIs
  _onGetCurrentLocation();
}

function _onGetCurrentLocation () {
    const options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
    };
    navigator.geolocation.getCurrentPosition( function (position) {
        //use coordinates
        const marker = { 
          lat: position.coords.latitude, 
          lng: position.coords.longitude 
        };
    }, function (error) {
      //error handler here
    }, options)
}

关于javascript - iOS Safari上javascript中的地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293914/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4