[android] Create an Android GPS tracking application

Recently, I've taken up android development as a hobby and was looking to develop an application that can find and track a users position using Google Maps.

Once the application has a GPS lock, it can track their movements by drawing a route using an overlay class.

I've seen similar applications like Mytracks that are open source but they're too complex for me right now.

Ideally i'd love to create an application that looks like this

Here is my code below without the imports.

What I'm trying to do is create an array of geopoints. Every time the location changes a new geopoint is created. I then try to use a for loop to iterate through each geopoint and draw a path between them.

public class Tracking extends MapActivity implements LocationListener {

LocationManager locman;
LocationListener loclis;
Location Location;
private MapView map;

List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    initMapView();
    initMyLocation();
    locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // locman.requestLocationUpdates(provider,60000, 100,loclis);
    // Location = locman.getLastKnownLocation(provider);

}

/** Find and initialize the map view. */
private void initMapView() {
    map = (MapView) findViewById(R.id.map);
    controller = map.getController();
    map.setSatellite(false);
    map.setBuiltInZoomControls(true);
}

/** Find Current Position on Map. */
private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass(); // does not work in emulator
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            // Zoom in to current location
            controller.setZoom(24);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);
}

@Override
public void onLocationChanged(Location location) {
    if (Location != null) {
        lat = Location.getLatitude();
        lon = Location.getLongitude();
        GeoPoint New_geopoint = new GeoPoint((int) (lat * 1e6),
                (int) (lon * 1e6));
        controller.animateTo(New_geopoint);

    }

}

class MyOverlay extends Overlay {
    public MyOverlay() {
    }

    public void draw(Canvas canvas, MapView mapv, boolean shadow) {
        super.draw(canvas, mapv, shadow);

        Projection projection = map.getProjection();
        Path p = new Path();
        for (int i = 0; i < geoPointsArray.size(); i++) {
            if (i == geoPointsArray.size() - 1) {
                break;
            }
            Point from = new Point();
            Point to = new Point();
            projection.toPixels(geoPointsArray.get(i), from);
            projection.toPixels(geoPointsArray.get(i + 1), to);
            p.moveTo(from.x, from.y);
            p.lineTo(to.x, to.y);
        }
        Paint mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(0xFFFF0000);
        mPaint.setAntiAlias(true);
        canvas.drawPath(p, mPaint);
        super.draw(canvas, map, shadow);
    }
}

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

}

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

}

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

}

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

The application runs fine without errors but there is no path drawn, the location dot just moves as i move.

Any help would be greatly appreciated Thanks.

This question is related to android dictionary gps tracking

The answer is


Basically you need following things to make location detector android app

Now if you write each of these module yourself then it needs much time and efforts. So it would be better to use ready resources that are being maintained already.

Using all these resources, you will be able to create an flawless android location detection app.

1. Location Listening

You will first need to listen for current location of user. You can use any of below libraries to quick start.

Google Play Location Samples

This library provide last known location, location updates

Location Manager

With this library you just need to provide a Configuration object with your requirements, and you will receive a location or a fail reason with all the stuff are described above handled.

Live Location Sharing

Use this open source repo of the Hypertrack Live app to build live location sharing experience within your app within a few hours. HyperTrack Live app helps you share your Live Location with friends and family through your favorite messaging app when you are on the way to meet up. HyperTrack Live uses HyperTrack APIs and SDKs.

2. Markers Library

Google Maps Android API utility library

  • Marker clustering — handles the display of a large number of points
  • Heat maps — display a large number of points as a heat map
  • IconGenerator — display text on your Markers
  • Poly decoding and encoding — compact encoding for paths, interoperability with Maps API web services
  • Spherical geometry — for example: computeDistance, computeHeading, computeArea
  • KML — displays KML data
  • GeoJSON — displays and styles GeoJSON data

3. Polyline Libraries

DrawRouteMaps

If you want to add route maps feature in your apps you can use DrawRouteMaps to make you work more easier. This is lib will help you to draw route maps between two point LatLng.

trail-android

Simple, smooth animation for route / polylines on google maps using projections. (WIP)

Google-Directions-Android

This project allows you to calculate the direction between two locations and display the route on a Google Map using the Google Directions API.

A map demo app for quick start with maps


The source code for the Android mobile application open-gpstracker which you appreciated is available here.

You can checkout the code using SVN client application or via Git:

Debugging the source code will surely help you.


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to dictionary

JS map return object python JSON object must be str, bytes or bytearray, not 'dict Python update a key in dict if it doesn't exist How to update the value of a key in a dictionary in Python? How to map an array of objects in React C# Dictionary get item by index Are dictionaries ordered in Python 3.6+? Split / Explode a column of dictionaries into separate columns with pandas Writing a dictionary to a text file? enumerate() for dictionary in python

Examples related to gps

Android "gps requires ACCESS_FINE_LOCATION" error, even though my manifest file contains this Best way to get user GPS location in background in Android How to set fake GPS location on IOS real device Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location How to get current location in Android How to get Android GPS location Is Android using NTP to sync time? Does GPS require Internet? Android: How to get accurate altitude? Get GPS location via a service in Android

Examples related to tracking

Create an Android GPS tracking application