3

Possible Duplicate:
Is there an API for Google Maps navigation in Android?

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, The application 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.

He 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.

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

enter image description here

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;
   }}
Community
  • 1
  • 1
sam_k
  • 5,983
  • 14
  • 76
  • 110

1 Answers1

0

Read my answer HERE.

What you are looking at is to create an android service that is fired each time ur location changes and records the new location, probably to a db. Also create an app that will use data from the db to draw a path on a map-view.

Community
  • 1
  • 1
Ujwal Parker
  • 682
  • 7
  • 11