地理定位太慢了!我做错了什么?
这是我在测试页面中使用的简单代码:但需要很长时间才能找到地址......怎么会呢?我做错了什么吗?
<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>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯——它实际上是在进行地理定位!
为了加快速度,请考虑提供额外的参数来利用缓存的结果和超时。
Well - it's actually doing geolocation!
To speed it up, consider providing the extra parameters for utilising cached results, and a timeout.
我已经为此苦苦挣扎了几个小时,结果发现每个浏览器都有不同的结果,也就是代码无法完全控制的东西,但您可以通过将这些选项添加到
getlocation.getCurrentPosition
函数调用:您可以详细了解每个选项的含义 此处
要了解使用 Chrome 的基准测试:
注意:第一次调用其中每一个时,仍然需要大约 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:You can read more about the meaning of each option here
To get a sense of benchmarks using Chrome:
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 removingenableHighAccuracy
.检查您的移动设备的位置服务模式。确保您的设备上的准确性处于“高”状态。高精度模式使用 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.
您的一些调用可能需要几秒钟才能完成,例如,在 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.