Meteor Js : How to take the user location by browser ?

Often we need to get the user’s location through the browser. I implemented in my Meteor Js application  to get this information when it is accessed by the user.

In this case we must put the function inside:

Meteor.startup ({..});

The function that you need is :

 navigator.geolocation.getCurrentPosition(function    (position) {
         {...}
 });

See my example below. I’m taking the data and I setting it in the session to have access into all application.

Example:

Meteor.startup(function() {
    if (Session.get('lat') == undefined 
             || Session.get('lon') == undefined) {
        navigator.geolocation.getCurrentPosition(function(position) {
            Session.set('lat', position.coords.latitude);
            Session.set('lon', position.coords.longitude);
        });
    }
});

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s