starting with android gps
Hello everyone, it’s been a while since my last post.It’s bcz i’ve been too lazy last few weeks figuring out what to do or what not.Though it’s not bcz of me it’s bcz my last semester exam is nearby and i have so much to do and so much to learn,Do you know the feeling when you are too excited to do lot more than you are thinking and you can not do anything at all, well i am in that situation right now.Let’s hope nothing goes wrong.
So, today i am going talk about android gps.
I am not going to dig into the theory behind android gps,rather i will try to show how you can begin development with android gps.
At first download this simple project so that you can understand what am i mockering.
WhereAmI
Open the projects code, at first, let’s see what happening in onCreate method
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
There may be different types of location services available in your device, to access those service we need a manager LocationManager. In this portion of the code We are getting the handle of LOCATION_SERVICE to manager, that is LocationManager.
String provider = LocationManager.GPS_PROVIDER;
Location location =
locationManager.getLastKnownLocation(provider);
Now that, we have the handler, we try to see current location by GPS service of that device,(you can also access other services using other providers (only if that’s available on your device)). Since we have the GPS_PROVIDER, now all we have to do is calling some method to see what gps is providing for us.Here we are trying to get the last known location.
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
This portion of the project is most important for us since it is responsible for listening the changes made by the device’s gps.
As you can see LocationManager has a method called requestLocationUpdates(java.lang.String provider, long minTime, float
minDistance, android.location.LocationListener listener) which takes as you can guess, 4 parameter.which provider to use,minimum time before listening for changes, minimum change in distance , and the listener.
Let’s see how is that listener is implemented:
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
here we have 4 methods but what we need for this project is onLocationChanged(Location location) and onProviderDisabled(String provider).
Every-time device changes it’s location, listener will fire the onLocationChanged(Location location) method with providing the location.
now remain only is updateWithNewLocation(location) which tells our UI to update with the location.
myLocationText.setText("Your Current Position is:\n" +
latLongString);
Before you run this project you should do a simple modification in your manifest file.
add <uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/> in your manifest. Use android.permission.ACCESS_COARSE_LOCATION if you are going to use LocationManager.NETWORK_PROVIDER.
And that’s it,wait, don’t run it right-away, we have a little housework to do.Now the Q is how the heck emulator is going to fetch the location, well that’s when mock gps comes in. It will mock you with gps location
, well not you, i mean emulator.
Fire up your emulator, after loading the emulator open your eclipse ide and go to ddms perspective view, you can see lot’s of thing happening in here. Find the emulator control.
now, i have provided a kml file with this project which stores some places location with their latitude and longitude.from emulator control’s kml “load KML” and locate the kml file.
Now run the project, after being initiated it will not show you anything bcz gps is not reading anything till now.Run the kml file from emulator control and voila, you are up and running.Isn’t it cool, you are moving from one place another without
being moved for an inch from your computer.I think that’s awesome.:)
Thanks everybody, next i will try to post something about android ndk(takes whole lot of pain only for configuring!!!) or
maybe else till then “HAVE A GOOD PROGRAMMING”






last comment