In modern day handsets, there are a ton of sensors from which we can gather data to make our Android applications more rich and useful. Your smartphone likely has multiple sensors, from an accelerometer, to a gyroscope, to a GPS sensor. The following discussion will be on the latter. Today I will go over how to access location data from the phone’s GPS. This post will lead into the discussion next week, where I will outline how to use this data to grab and parse an XML document from a RESTful API. More on that next week. So, let’s get started.

 

  1. The first thing we must do before accessing any of these sensors is to set the permissions of our app so that the device will allow our application to access said sensors. You must check several permissions. Fine location and coarse location should both be checked. The difference between these is that fine location will be more granular and therefore more accurate than course location data. You must also have the internet permission checked. This permission is set in case the user of the application does not have a GPS in their device and allows the application to get their location data from the network. The GPS will produce fine location data, whereas the network will produce course location data.

 

  1. Next, for the current activity, we will implement ILocationListener which will allow us to listen to changes in the location from the GPS. Then, I declare a few variables. For this discussion, I will omit talking about the first two for now. We need to create an instance of the LocationManager which we will use later, as well as a variable for the provider.

  1. Next, inside of our activity’s OnCreate procedure, we cast our locationManager as LocationManager, and use it to access the LocationService. And set the Criteria for that LocationService. Notice here that I have set my accuracy to coarse. You could just as well set it to fine, and you would wind up with more accurate location data.

  1. Now I create a list of acceptable service providers for my device. This list will get the sensors that store location data for my device. In my If statement, I tell the locationManager that I want the first provider from my list of acceptable providers. In phones that have a GPS, the first acceptable provider will be that sensor. Others that may not have this sensor, the first acceptable provider will likely be the network.

  1. In this next piece, I am getting the data from my device’s GPS sensor. I set my Location variable as locCurrent and request location updates from my locationProvider, which in this case is my GPS. If you are testing on a device that does not have a GPS, you simply need to replace LocationManager.GpsProvider with LocationManager.NetworkProvider. Now, you will notice that I am passing the longitude and latitude properties of locCurrent to my currentURL variable that I declared earlier. This URL will be discussed further next week. For now, just know that we can get this information from our GPS. We can also get other information, if available, such as altitude, bearing, and speed.

  1. The last part here is to initiate the events that are necessary when we implement ILocationListener. These events are OnLocationChanged, OnProviderDisabled, OnProviderEnabled, and OnStatusChanged. In my example I am not doing much with these, I am merely implementing them because they are required by ILocationListener. However, if for instance, we had a TextView that we wanted to update every time the location changed with the current longitude, latitude, and altitude, we could certainly implement that within the OnLocationChanged event handler.

That’s it for this tutorial on accessing the location data from your device’s GPS. Now, this is not extremely useful just yet. However, next week I will show how we can use this data to get some useful information from a RESTful API. So, stay tuned for a continuation of this series next week. Remember, to comment below with your thoughts. If you liked this content, please help spread the knowledge by sharing on your favorite platform by clicking one of the buttons below. As always, happy programming!

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s