我正在开发一个项目,以MapView
根据GPS
Provider 的当前位置显示路线。我能够绘制两点之间的路径,但是当位置更改为导致绘制路径被擦除的新点时,问题就开始了。
基本上我的位置监听器保存当前位置GeoPoint
并设置新GeoPoint
位置,然后它开始覆盖以根据这两个点绘制路径。我认为一个选项可以将坐标保存到数据库,然后从那里提取信息进行绘制路径。
到目前为止,这是我的代码:
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Toast.makeText(getBaseContext(), "Tracking device..",
Toast.LENGTH_SHORT).show();
gp2 = new GeoPoint(
gp1.getLatitudeE6(),
gp1.getLongitudeE6());
gp1 = new GeoPoint(
(int) (location.getLatitude() * 1000000),
(int) (location.getLongitude() * 1000000));
myOverlay = new MapOverlay();
mapOverlays_route.add(myOverlay);
myMapView.invalidate();
}
}
class MapOverlay extends com.google.android.maps.Overlay{
public MapOverlay(){
}
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);
Point p1 = new Point();
Point p2 = new Point();
Path path1 = new Path();
Path path2 = new Path();
projection.toPixels(gp1, p1);
projection.toPixels(gp2, p2);
path1.moveTo(p2.x, p2.y);//current location
path1.lineTo(p1.x,p1.y);//new location
canvas.drawPath(path1, mPaint);//drawing the path
}
}