javascript 地理定位太慢了!我做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3752383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geolocation is so slow! what I'm doing wrong?
提问by Francesco
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>
采纳答案by Galen
I've found that the speeds are a lot different depending on the browser. I've been testing my geolocation with chrome, because that is almost instant. Firefox is slow as hell (lots of the time it doesnt even work), and safari is in second. Hopefully in time they will fix their implementation so it's as fast as chrome's
我发现浏览器的速度差异很大。我一直在用 chrome 测试我的地理定位,因为这几乎是即时的。Firefox 慢得要命(很多时候它甚至不起作用),而 safari 排在第二位。希望他们能及时修复他们的实现,所以它和 chrome 一样快
回答by Chris Broadfoot
Well - it's actually doing geolocation!
好吧 - 它实际上是在做地理定位!
To speed it up, consider providing the extra parameters for utilising cached results, and a timeout.
为了加快速度,请考虑提供用于利用缓存结果的额外参数和超时。
回答by ganeshkumar
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.
检查您的移动设备的位置服务模式。确保准确性在您的设备上处于高状态。高精度模式使用 GPS、WIFI 或移动网络来查找位置。
If you choose Low or GPS only mode it takes forever.
如果您选择低或仅 GPS 模式,则需要永远。
Also play with the geo location option and give the timeout and cache options.
还可以使用地理位置选项并提供超时和缓存选项。
回答by Hossam Mourad
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.getCurrentPositionfunction call:
我已经为此苦苦挣扎了几个小时,结果发现每个浏览器都有不同的结果,也就是代码无法完全控制的东西,但您可以通过将这些选项添加到getlocation.getCurrentPosition函数调用来取得一些进展:
enableHighAccuracy: false,
timeout: 5000,
maximumAge: Infinity
You can read more about the meaning of each option here
您可以在此处阅读有关每个选项含义的更多信息
回答by adamse
A couple of your calls might take some seconds to finish, for example navigator.geolocation.getCurrentPositiontakes up to 5 sec (when working) for me in Safari.
您的几个电话可能需要几秒钟才能完成,例如navigator.geolocation.getCurrentPosition在 Safari 中对我来说最多需要 5 秒(工作时)。

