无法应对 navigator.geolocation 的异步特性
我在 firefox 3.6 中使用 navigator.geolocation.getCurrentPosition(function) api。当我尝试重复调用此方法时,我发现有时它有效,有时则无效。我认为问题是因为它的异步回调性质。我可以看到回调函数在某个时刻被调用,但我的外部函数已经退出,所以我无法捕获位置坐标的值。
我对 javascript 还很陌生,所以我假设其他 javascript 编码人员可能已经知道如何处理它。请帮忙。
的一段示例代码
<script type="text/javascript">
function getCurrentLocation() {
var currLocation;
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}
return currLocation; // this returns undefined sometimes. I need help here
}
</script>
编辑:这是我正在使用编辑2 : 感谢大家的回答,我希望我可以选择所有答案为“已接受”,但不能这样做。
现在我面临另一个问题。我每 3 秒调用 navigator.geolocation.getCurrentPosition,但响应在 10 - 15 个回复后停止。有人知道吗?
再次感谢
I'm using the navigator.geolocation.getCurrentPosition(function) api in firefox 3.6. When i try to call this method repeatedly I see that sometimes it works and sometimes it doesn't. I figured that the problem is because of its asynchronous callback nature. I can see that the callback function is being called at some point but my outer function is already exiting so i cannot catch the values of the position coordinates.
I'm pretty new to javascript so i'm assuming other javascript coders might have already figured out how to deal with it. Please help.
Edit: here's a sample piece of code i'm using
<script type="text/javascript">
function getCurrentLocation() {
var currLocation;
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
});
}
return currLocation; // this returns undefined sometimes. I need help here
}
</script>
Edit 2:
Thanks everyone for answering, I wish i could choose all the answers as "accepted" but cannot do so.
Now I'm facing another problem. I'm calling the navigator.geolocation.getCurrentPosition every 3 seconds, but the responses stop after 10 - 15 replies. Any one got any idea ?
thanks again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用承诺:
You can use Promise:
是的,您对操作的回调性质有疑问。您不能调用
getCurrentLocation()
函数并期望它会同步返回。我什至感到惊讶的是它偶尔会起作用。在处理异步调用时,您必须使用稍微不同的范例。您可能应该调用函数
plotCurrentLocation()
并执行类似以下示例的操作:注意
map
参数如何传递给plotCurrentLocation()
函数可供内部函数使用。这是有效的,因为 JavaScript 有闭包。更新:
其他答案建议的回调方法是通过添加另一层抽象来解决此问题的另一种选择。
Yes you have a problem with the callback nature of the operation. You cannot call the
getCurrentLocation()
function and expect that it will return synchronously. I am even surprised that it did work occasionally.You have to use a slightly different paradigm when working with asynchronous calls. You should probably call your function,
plotCurrentLocation()
and do something like the following example:Note how the
map
parameter passed to theplotCurrentLocation()
function is available to the inner functions within. This works because JavaScript has closures.UPDATE:
The callback method suggested by the other answers is another option to tackle this, by adding another layer of abstraction.
你试图让它同步,但它不会起作用。正如您所看到的,不能保证 currLocation 在函数返回时被设置。您现在可能有类似的内容:
将您的函数更改为:
并将客户端代码更改为:
You're trying to make it synchronous, and it won't work. As you've seen, there is no guarantee currLocation is set when the function returns. You probably now have something like:
Change your function to:
and the client code to:
更好地使用:
...
Better to use:
...
您还可以为
getCurrentPosition
编写一个包装函数,这允许您选择如何处理它(
async/await
、then()
,无论如何);例如You could also write a wrapper function for
getCurrentPosition
This allows you to choose how to handle it (
async/await
,then()
, whatever); for example