菜鸟教程小白 发表于 2022-12-11 19:36:59

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(&#34;API geolocation success!\n\nlat = &#34; + position.coords.latitude + &#34;\nlng = &#34; + position.coords.longitude);
};

var tryAPIGeolocation = function() {
    jQuery.post( &#34;https://www.googleapis.com/geolocation/v1/geolocate?key=&#34;MY API KEY&#34;, function(success) {
      apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
    alert(&#34;API Geolocation error! \n\n&#34;+err);
});
};

var browserGeolocationSuccess = function(position) {
    alert(&#34;Browser geolocation success!\n\nlat = &#34; + position.coords.latitude + &#34;\nlng = &#34; + position.coords.longitude);
};

var browserGeolocationFail = function(error) {
switch (error.code) {
    case error.TIMEOUT:
      alert(&#34;Browser geolocation error !\n\nTimeout.&#34;);
      break;
    case error.PERMISSION_DENIED:
      if(error.message.indexOf(&#34;Only secure origins are allowed&#34;) == 0) {
      tryAPIGeolocation();
      }
      break;
    case error.POSITION_UNAVAILABLE:
      alert(&#34;Browser geolocation error !\n\nPosition unavailable.&#34;);
      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 &amp;&amp; navigator.permissions.query) {
//try permissions APIs first
navigator.permissions.query({ name: &#39;geolocation&#39; }).then(function(result) {
      // Will return [&#39;granted&#39;, &#39;prompt&#39;, &#39;denied&#39;]
      const permission = result.state;
      if ( permission === &#39;granted&#39; || permission === &#39;prompt&#39; ) {
          _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]
查看完整版本: javascript - iOS Safari上javascript中的地理定位