javascript - iOS Safari上javascript中的地理定位
<p><p>我的目标是在 iOS 设备上使用 Safari 返回我的地理位置坐标。在我的桌面浏览器上,我使用 Google Maps Geolocation APIkey 调用下面的函数 <code>tryGeolocation()</code>。 </p>
<p>这<b>在桌面 Chrome 上有效</b>,但在 Safari(桌面和 iOS)和 Chrome (iOS) 中,我返回 <code>"Position Unavailable"</code> 错误。我尝试过 5 秒和 10 秒的超时,但同样的情况发生了。 </p>
<p>是否有在 iOS 中调用 Google Maps Geolocation API 的已知解决方法? </p>
<pre><code>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();
</code></pre>
<p>我是使用 iOS 浏览器的新手,所以任何建议都将不胜感激!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这里是 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API" rel="noreferrer noopener nofollow">Permissions</a> 的示例和 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition" rel="noreferrer noopener nofollow">Navigation</a>涉及的 API</p>
<p>请记住,它<strong>仅当</strong>主机使用 <strong>HTTPS</strong> 时才有效。
这可能是由于您对以下答案的评论而导致权限警报未显示的原因。</p>
<p>请记住,如果您的主机不使用 HTTPS,它将超时并出现有关身份验证失败的错误。使用错误回调来检查。</p>
<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>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)
}</code></pre>
<p style="font-size: 20px;">关于javascript - iOS Safari上javascript中的地理定位,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48293914/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48293914/
</a>
</p>
页:
[1]