使用W3C GeoLocation API的watchPosition函数 [英] Using the W3C GeoLocation API's watchPosition function

查看:161
本文介绍了使用W3C GeoLocation API的watchPosition函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使用W3C GeoLocation API来观看用户的位置。最简单的方法是什么?我只需要更新用户的经度和纬度。

I cannot work out how to watch a user's location using the W3C GeoLocation API. What's the easiest way? All I want is an update of the user's latitude and longitude.

推荐答案

我刚刚在一个项目中使用了这个使用本Nadel的博客帖子和API本身。

I've just been using this in a project, I worked it out using Ben Nadel's blogpost and the API itself.

navigator.geolocation.watchPosition(function(position){

    // These variables update every time the location changes
    var Latitude = position.coords.latitude;
    var Longitude = position.coords.longitude;

}, function(error){
    // You can detect the reason and alert it; I chose not to.   
    alert('We could not get your location');
},{
    // It'll stop looking after an hour. Enabling high accuracy tells it to use GPS if it's available  
    timeout: 5000,
    maximumAge: 600000,
    enableHighAccuracy: true
});

希望这有助于您!

这篇关于使用W3C GeoLocation API的watchPosition函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆