iOS Safari 上的 javascript 中的地理位置



我的目标是在iOS设备上使用Safari返回我的地理位置坐标。 在我的桌面浏览器上,我使用谷歌地图地理位置API密钥调用下面的tryGeolocation()函数。

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

在iOS中调用谷歌地图地理位置API是否有已知的解决方法?

var apiGeolocationSuccess = function(position) {
    alert("API geolocation success!nnlat = " + 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! nn"+err);
  });
};
var browserGeolocationSuccess = function(position) {
    alert("Browser geolocation success!nnlat = " + position.coords.latitude + "nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
  switch (error.code) {
    case error.TIMEOUT:
      alert("Browser geolocation error !nnTimeout.");
      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 !nnPosition unavailable.");
      break;
  }
};
var tryGeolocation = function() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        browserGeolocationSuccess,
      browserGeolocationFail,
      {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
  }
};
tryGeolocation();

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

下面是

涉及权限和导航 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)
}

if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          pos = {
                        lat: position.coords.latitude,
                        lng: position.coords.longitude
          };
          latt=pos.lat;
          lngg=pos.lng;
          console.log("point :"+latt+","+lngg);
        }, function() {
        });
      } else {
        // Browser doesn't support Geolocation
      }

      this.restapiService.getAllSearchResults()
      .then(data => {
              this.map = new google.maps.Map(this.mapElement.nativeElement, {
                zoom: 100,
                center: {lat: parseFloat(pos.lat), lng: parseFloat(pos.lng)}
              });
        var position = new google.maps.LatLng(parseFloat(pos.lat), parseFloat(pos.lng));
        var sMarker = new google.maps.Marker({position: position,animation: google.maps.Animation.DROP, title: "My Location"});
        sMarker.setMap(this.map);
      });
    }

您可以尝试此操作以获取当前位置[在iOS(浏览器模拟器ionic(上测试]

尝试不使用 getCurrentPosition(( 的第三个参数,因为我们有错误问题。POSITION_UNAVAILABLE。

var apiGeolocationSuccess = function(position) {
    alert("API geolocation success!nnlat = " + 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! nn"+err);
  });
};
var browserGeolocationSuccess = function(position) {
    alert("Browser geolocation success!nnlat = " + position.coords.latitude + "nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
  switch (error.code) {
    case error.TIMEOUT:
      alert("Browser geolocation error !nnTimeout.");
      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 !nnPosition unavailable.");
      break;
  }
};
        var tryGeolocation = function() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            browserGeolocationSuccess,
          browserGeolocationFail);
      }
    };
    tryGeolocation();

相关内容

  • 没有找到相关文章

最新更新