代码之家  ›  专栏  ›  技术社区  ›  Francesco

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

  •  24
  • Francesco  · 技术社区  · 15 年前

    这是我在测试页面中使用的简单代码:但是找到地址要花很长时间…为什么?我做错什么了吗?

    <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>
    
    5 回复  |  直到 13 年前
        1
  •  11
  •   Chris Broadfoot    15 年前

    它实际上是在做地理定位!

        2
  •  3
  •   ganeshkumar    11 年前

    检查移动设备的位置服务模式。确保在你的设备上准确度是高的。高精度模式使用GPS、WIFI或移动网络来定位。

    还可以使用geo location选项,并提供timeout和cache选项。

        3
  •  1
  •   Hossam Mourad    7 年前

    我已经为此挣扎了几个小时了,结果发现每个浏览器的结果都不一样,代码无法完全控制它,但您可以通过添加这些选项来取得一些进展 getlocation.getCurrentPosition

    enableHighAccuracy: false,
    timeout: 5000,
    maximumAge: Infinity
    

    你可以阅读更多关于每个选项的含义 here

        4
  •  0
  •   ase    15 年前

    navigator.geolocation.getCurrentPosition 我在Safari中最多需要5秒(工作时)。