地理定位太慢了!我做错了什么?

发布于 2024-09-24 01:06:14 字数 1985 浏览 4 评论 0原文

这是我在测试页面中使用的简单代码:但需要很长时间才能找到地址......怎么会呢?我做错了什么吗?

<script src="http://maps.google.com/maps?hl=it&amp;file=api&amp;v=2&amp;sensor=true&amp;key=*xxxxxx*" type="text/javascript"></script>
<script type="text/javascript">
    var map;
    var geocoder;

    function addAddressToMap(response) 
    {
      if (!response || response.Status.code != 200) 
      {
        alert("Sorry, we were unable to geocode that address");
      } 
      else 
      {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

        document.getElementById('address').innerHTML = place.address;
      }
    }


    function searchGeolocation() 
    {
        if (navigator.geolocation) 
        {
            navigator.geolocation.getCurrentPosition(function(position) 
            {  
                geocoder = new GClientGeocoder();
                document.getElementById('latitude').innerHTML = position.coords.latitude;
                document.getElementById('longitude').innerHTML = position.coords.longitude;
                coordinates = position.coords.latitude+","+position.coords.longitude;
                geocoder.getLocations(coordinates, addAddressToMap);

            }); 
        }else
        {
            document.getElementById('latitude').innerHTML = "Unknown";
            document.getElementById('longitude').innerHTML = "Unknown";
            document.getElementById('address').innerHTML = "Unknown";
            alert("I'm sorry, but geolocation services are not supported by your browser.");    
        }
    }



</script>


<br/>
latitude = <div id="latitude">loading...</div>
<br/>
longitude = <div id="longitude">loading...</div>
<br/>
address = <div id="address">loading...</div>
<br/>


<script type="text/javascript">

    searchGeolocation();

</script>

this is my simple code I'm using in a test page: but it takes ages to find the address...how come? am i doing something wrong?

<script src="http://maps.google.com/maps?hl=it&file=api&v=2&sensor=true&key=*xxxxxx*" type="text/javascript"></script>
<script type="text/javascript">
    var map;
    var geocoder;

    function addAddressToMap(response) 
    {
      if (!response || response.Status.code != 200) 
      {
        alert("Sorry, we were unable to geocode that address");
      } 
      else 
      {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

        document.getElementById('address').innerHTML = place.address;
      }
    }


    function searchGeolocation() 
    {
        if (navigator.geolocation) 
        {
            navigator.geolocation.getCurrentPosition(function(position) 
            {  
                geocoder = new GClientGeocoder();
                document.getElementById('latitude').innerHTML = position.coords.latitude;
                document.getElementById('longitude').innerHTML = position.coords.longitude;
                coordinates = position.coords.latitude+","+position.coords.longitude;
                geocoder.getLocations(coordinates, addAddressToMap);

            }); 
        }else
        {
            document.getElementById('latitude').innerHTML = "Unknown";
            document.getElementById('longitude').innerHTML = "Unknown";
            document.getElementById('address').innerHTML = "Unknown";
            alert("I'm sorry, but geolocation services are not supported by your browser.");    
        }
    }



</script>


<br/>
latitude = <div id="latitude">loading...</div>
<br/>
longitude = <div id="longitude">loading...</div>
<br/>
address = <div id="address">loading...</div>
<br/>


<script type="text/javascript">

    searchGeolocation();

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淤浪 2024-10-01 01:06:15

嗯——它实际上是在进行地理定位!

为了加快速度,请考虑提供额外的参数来利用缓存的结果和超时。

Well - it's actually doing geolocation!

To speed it up, consider providing the extra parameters for utilising cached results, and a timeout.

南冥有猫 2024-10-01 01:06:15

我已经为此苦苦挣扎了几个小时,结果发现每个浏览器都有不同的结果,也就是代码无法完全控制的东西,但您可以通过将这些选项添加到 getlocation.getCurrentPosition 函数调用:

enableHighAccuracy: false,
timeout: 5000,
maximumAge: Infinity

您可以详细了解每个选项的含义 此处

要了解使用 Chrome 的基准测试:

enableHighAccuracy: true // ~3997ms
enableHighAccuracy: false // ~84ms

注意:第一次调用其中每一个时,仍然需要大约 3.9 秒。通过使用 maximumAge 或删除 enableHighAccuracy 可以减少每个后续调用。

I've been struggling with that for a couple of hours now and it turned out that it is a different result for each browser aka it is something that code can't fully control but you can do some progress by adding these options to getlocation.getCurrentPosition function call:

enableHighAccuracy: false,
timeout: 5000,
maximumAge: Infinity

You can read more about the meaning of each option here

To get a sense of benchmarks using Chrome:

enableHighAccuracy: true // ~3997ms
enableHighAccuracy: false // ~84ms

Note: the first time each of these is called, it's still approximately 3.9 seconds. Each subsequent call can be reduced by using the maximumAge or removing enableHighAccuracy.

抽个烟儿 2024-10-01 01:06:15

检查您的移动设备的位置服务模式。确保您的设备上的准确性处于“高”状态。高精度模式使用 GPS、WIFI 或移动网络来查找位置。

如果您选择“低”或“仅 GPS”模式,则需要很长时间。

还可以使用地理位置选项并提供超时和缓存选项。

Check your mobile device's Location service mode. Make sure the accuracy is in High status on your devide. The High accuracy mode uses GPS, WIFI or Mobile network to find the location.

If you choose Low or GPS only mode it takes forever.

Also play with the geo location option and give the timeout and cache options.

走走停停 2024-10-01 01:06:15

您的一些调用可能需要几秒钟才能完成,例如,在 Safari 中,navigator.geolocation.getCurrentPosition 最多需要 5 秒(工作时)。

A couple of your calls might take some seconds to finish, for example navigator.geolocation.getCurrentPosition takes up to 5 sec (when working) for me in Safari.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文