当前位置: 动力学知识库 > 问答 > 编程问答 >

Meteor geo location from browser package

问题描述:

I've tried searching atmosphere and the web for any package to get users Geo location (latitude, longitude) while accessing the app using their desktop browsers but couldn't find any. Existing applications serve mobile only ex. MDG Geo Location Can someone please tell me how to get the user geo location (latitude, longitude) from desktop through Meteor? Thanks

网友答案:

I know this is an old post, however, thought of putting this through.

You can try using the gunjansoni:html5-api package. It has geo location and lot of other HTML5 Apis to play with.

After you install the package on you client

var html5api = new Html5Api();

and then request for geo location permissions:

var geoLocation = html5api.geoLocation();

Once you have the permissions, get the location.

if (geoLocation) {
    geoLocation.getLocation(function(err, res){
        if (err) console.log(err);
        else console.log(res);
    });
}

The result will be something like this:

{
    accuracy: 30 // in meters
    altitude: null
    altitudeAccuracy: null
    heading: null
    latitude: <latitude>
    longitude: <longitude>
    speed: null
    timestamp: 1457901207220
}

Or you can watch a user's location

if (geoLocation) {
    geoLocation.watch(function (err, res) {
        if (err) console.log(err);
        else {
            // the result will be in res.result
            console.log(res.result);
            Meteor.setInterval(function () {
                // to stop watching a location, you need a watchId which is available in res.watchId when the watch started
                geoLocation.stopWatching(res.watchId);
            }, 30 * 1000);
        }
    });
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: