I want to track user's positions using GPS and draw lines between visited GPS points.
My code draw one time, then it doesn't draw
public class Test extends Activity {
float lat1, lon1 = 0;
float lat0, lon0 = 0;
private LocationManager locManager;
private LocationListener locListener;
private MapController mc;
private MapView mapView;
private MyOverlay myoverlay;
private Projection projection;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracklocation);
mapView = (MapView) findViewById(R.id.map);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new MylocListener();
GeoPoint initGeoPoint = new GeoPoint((int) ( locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude() * 1000000 ),
(int) ( locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude() * 1000000 ));
Toast.makeText(getBaseContext(),
"Initial Latitude: " + locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude() * 1000000 +
"Initial Longitude: " + locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude() * 1000000,
Toast.LENGTH_LONG).show();
lat0 = (float) locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude() * 1000000;
lon0 = (float) locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude() * 1000000;
GeoPoint gPe = new GeoPoint((int) lat0, (int) lon0);//starting point Abbottabad
mc.animateTo(gPe);
mc.setZoom(16);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
public class MylocListener implements LocationListener {
public void onLocationChanged(Location arg0) {
GeoPoint gP2 = new GeoPoint(
(int) ( arg0.getLatitude() * 1E6 ),
(int) ( arg0.getLongitude() * 1E6 ));
Toast.makeText(getBaseContext(),
"Latitude: " + arg0.getLatitude() +
" Longitude: " + arg0.getLongitude(),
Toast.LENGTH_LONG).show();
lat1 = (float) gP2.getLatitudeE6() / 1000000;
lon1 = (float) gP2.getLongitudeE6() / 1000000;
MyOverlay mapOverlay = new MyOverlay();
projection = mapView.getProjection();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
class MyOverlay extends Overlay {
public MyOverlay() {
}
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
//Configuring the paint brush
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
GeoPoint gPe = new GeoPoint((int) lat0, (int) lon0);//starting point Abbottabad
GeoPoint gP2 = new GeoPoint((int) lat1, (int) lon1);//starting point Abbottabad
Point p1 = new Point();
Point p2 = new Point();
Path path1 = new Path();
projection.toPixels(gP2, p2);
projection.toPixels(gPe, p1);
mc.animateTo(gP2);
path1.moveTo(p2.x, p2.y);//Moving to Islamabad location
path1.lineTo(p1.x, p1.y);//Path to Rawalpindi
canvas.drawPath(path1, mPaint);
Toast.makeText(getBaseContext(),
"I've drawn" + (float) gP2.getLatitudeE6() / 1000000 +
(float) gP2.getLongitudeE6() / 1000000,
Toast.LENGTH_LONG).show();
gPe = new GeoPoint((int) lat1, (int) lon1);
}
}
}