You are looking for to know where the user of your application is exactly and in real time?

Here is a really simple and small tutorial to understand and implement an activity which get the location of the user in real time.

By location, I mean his longitude, latitude, but also the address.

If you want to make it scalable and keep your app scalable, do no hesitate to re-use this activity and make your activity extends of this one!

 

But First, let’s kick off which the location of the user… GO !

 

Step 1: The code

Here is what the activity looks like… BOOM ! (Don’t worry I explain all of these code below the screenshots)

 

Screen Shot 2015-03-24 at 14.09.14

Screen Shot 2015-03-24 at 14.09.20

Full source code here on Github.

 

Step 2: Explanation

 The secret? Make implements your activity (or your class) of the LocationListener interface !

 

Once your activity (or your class) implementing LocationListener, you will need to override the methods OnLocationChanged, onProviderEnabled, onProviderDisabled.

 

Only the method OnLocationChanged interests us in this case.

The 2 others are useful to know if one of your providers is enable or not. You can warn your user if the user looses its Internet connection for example.

 

 

OnLocationChanged

 OnLocationChanged takes in one parameter: a Location object.

Through this object, you get a Location object containing the longitude and the latitude of the user.

This callback is called every time the information inside the Location object is updated (every time then).

Inside the Location object there is a method getLongitude and getLatitude to get the information wanted.

From this information you can determinate the city name where the user is.

 

GetLocation

 I created a method called getLocation which initialize your location services and check if the 3G or the WIFI of the user is activated. Is so, I request the location of the user to this provider.

 

// if there is a GPS provider, request the location of the user
       if (_locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
           _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
       // if there is a Network provider, request the location of the user
       if (_locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
           _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

 

For both of the provider, I required the location known of the provider about the user.

This request return a Location user with the information, if the provider manages to give a location.

If not, your location will be null.

 

Location netLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
       if (netLocation != null)
       {
           onLocationChanged(netLocation);
       }

 

In my case, I want to know where the user is (his location, city name) so I call myself the onLocationChanged method in giving in parameter my Location object.

 

That’s all!

 

If you have planned to use a google map and display markers on it, I also wrote an article.

If you are looking for the location (longitude and latitude) of a place, but you only have the address, it is pretty easy and …. I also wrote an article for that !

 

Enjoy !!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>