Let's get to our main task: learn how to use GPS features in Android.
Before we start, make sure you have the following already installed:
1) Eclipse (freely available on-line)
2) Android SDK (freely available on-line)
Now lets create a Android Project:
Once you have created the project, it is time to create our first Java class and add it to the project:
Now that you have created a Java class make sure you override the onCreate() method:
Make sure to import the following classes:
import android.location.Criteria;
import android.location.LocationListener;
import android.location.LocationManager;
These contain all that is needed to request user location from your Android device. Now it is time to instantiate some objects that will request for the user location:
As a matter of better programming practices, I have created the following protected variables in my class:
protected LocationListener locationListener;
protected LocationManager locationManager;
It is up to you if you follow this tip or you can directly implement those in the onCreate() method. They have to be instantiated:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener(getApplicationContext());
Note: locationListener is a class that I implemented in order to keep things organized. If you want to follow the same practice, make sure you create a class MyLocationListener that implement LocationListener like this:
public class MyLocationListener implements LocationListener
{
//Methods that will be helpful to implement
/*
* Called when a new location is found by the network location provider. This overrides.
*/
public void onLocationChanged(Location location)
{
/*
* Make use of location obtained by device
*/
if(location != null)
{
//display location in latitude and longitud
text = "Lat: "+ location.getLatitude()
+ "\nLon: " + location.getLongitude() + "\n\n";
}
}
// You will have to override the
public void onStatusChanged(String provider, int status, Bundle extras)
{
/can be left blank
}
//and also
public void onProviderEnabled(String provider)
{
//can be left blank
}
//as well as
public void onProviderDisabled(String provider)
{
Toast toast = Toast.makeText(context, provider + " is disabled!", Toast.LENGTH_LONG);
toast.show();
}
}
Note: the text variable that is being used in the onLocationChanged above is a CharSequence variable that can be displayed using an EditText object or any other way you want to display your gps data.
You can also implement the following code that is used to START the service, this code goes in your class that contains the onCreate() method.
GPS_PROVIDER is used to get location just from GPS.
Or if you wish to get you location from the network you can use the following code:
Now that you have your locationManager running, you will need to stop the service at some point. This can be done anywhere you want but it has to be called once you do not want to continue collecting GPS data. The code looks like this:
This is pretty much what has to be done in order to obtain user location at any given time. The imports that we have used can be accessed on-line if you need more information on the parameters used in each instantiation.
For more information regarding this please refer to: Android API.
There is one last IMPORTANT detail that you need to take care if you don't want to find surprises. If you run that code as it is, you may notice that you get errors, and that is because you need to make a simple modification to the Manifest file. This file can be seen from Eclipse IDE as follows:
This is located in the "Package Explorer" in Eclipse. Open the AndroidManifest.xml and add the following code to the xml file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
This code is needed by the device in order to add the necessary permissions to use the GPS or Network.
Now you are ready to deploy your application to your device and start collecting GPS data that can be used to create location-based services. This coded is intended to give a brief introduction that will give you a push in your journey to the futuristic location-based services.








No comments:
Post a Comment