Home > Android > starting with android gps

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.

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 =

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,

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) {

public void onProviderDisabled(String provider){

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" +

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”

Categories: Android
  1. Ashish Kamath
    March 20, 2012 at 6:13 pm

    Hey i have a doubt in the following code snippet :

    import java.util.List;

    import android.os.Bundle;

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;
    import com.google.android.maps.Overlay;

    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Point;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;

    //import android.view.View;
    //import android.widget.LinearLayout;
    import android.widget.Toast;

    public class Current extends MapActivity
    private LocationManager lm;
    private LocationListener locationListener;
    GeoPoint p;
    private MapView mv;
    private MapController mc;

    public static final String GPS_PROVIDER=”gps”;

    class MapOverlay extends com.google.android.maps.Overlay

    public boolean draw(Canvas canvas, MapView mapView,
    boolean shadow, long when)
    super.draw(canvas, mapView, shadow);

    //—translate the GeoPoint to screen pixels—
    Point screenPts = new Point();
    mapView.getProjection().toPixels(p, screenPts);

    //—add the marker—
    Bitmap bmp = BitmapFactory.decodeResource(
    getResources(), R.drawable.marker);
    canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
    return true;

    public void add(CustomItemizedOverlay itemizedOverlay) {
    // TODO Auto-generated method stub


    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    //—use the LocationManager class to obtain GPS locations—
    lm = (LocationManager)

    locationListener = new MyLocationListener();

    LocationManager.GPS_PROVIDER, 0, 0, locationListener);


    protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;

    private class MyLocationListener implements LocationListener
    public void onLocationChanged(Location loc) {
    if (loc != null) {
    “Location changed : Lat: ” + loc.getLatitude() +
    ” Lng: ” + loc.getLongitude(),
    mv = (MapView) findViewById(R.id.mapView);
    mc = mv.getController();
    p = new GeoPoint(
    (int) (loc.getLatitude() * 1E6),
    (int) (loc.getLongitude() * 1E6));
    MapOverlay mapOverlay = new MapOverlay();
    List listOfOverlays = mv.getOverlays();



    public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

    public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

    public void onStatusChanged(String provider, int status,
    Bundle extras) {
    // TODO Auto-generated method stub

    i want the current location to be plotted on the map along with a marker using gps bt its not happening…plz help

  1. No trackbacks yet.

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

%d bloggers like this: