思路:将当前个人位置(经纬度信息)存储到手机数据库(SQLITE)中,然后遍历数据库中的所有位置信息,并绘制到MAP图层上。
1.数据库帮助类:
package com.example.touchpoint; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class MysqliteHelper extends SQLiteOpenHelper{ private static final String DATABASE_NAME = "my.db"; private static final int DATABASE_VERSION = 1; public static final String COLUMN_ID = "_id"; //必须的 public static final String COLUMN_LATITUDE = "latitude"; public static final String COLUMN_LONGITUDE = "longitude"; public static final String COLUMN_UPLOAD_TIME = "upload_time"; //暂时没用 public static final String[] allColumns = {COLUMN_ID,COLUMN_LATITUDE,COLUMN_LONGITUDE}; private static final String CREATE_TABLE = "create table if not exists routlist" + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_LATITUDE + " REAL not null, " + COLUMN_LONGITUDE + " REAL not null " + ");"; public MysqliteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { Log.d("CREATE_TABLE",CREATE_TABLE); db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table " + CREATE_TABLE); onCreate(db); } }
2.数据库DAO
package com.example.touchpoint; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class SqliteDAO { private SQLiteDatabase db; private MysqliteHelper helper; public SqliteDAO(Context context) { helper = new MysqliteHelper(context); } public void open() { db = helper.getWritableDatabase(); } public void close() { helper.close(); } public void writePositionToDB(double latitude,double longitude) { ContentValues values = new ContentValues(); values.put(MysqliteHelper.COLUMN_LATITUDE, latitude); values.put(MysqliteHelper.COLUMN_LONGITUDE, longitude); db.insert("routlist", null, values); } public List<LocationPoint> getAllPoints() { List<LocationPoint> points = new ArrayList<LocationPoint>(); Cursor cursor = db.query("routlist", MysqliteHelper.allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { LocationPoint point = cursorToPoint(cursor); points.add(point); cursor.moveToNext(); } // 记得关闭cursor对象 cursor.close(); return points; } private LocationPoint cursorToPoint(Cursor cursor) { LocationPoint point = new LocationPoint(); point.set_id(cursor.getLong(0)); point.setLatitude(cursor.getDouble(1)); point.setLongitutde(cursor.getDouble(2)); Log.d("route",point.toString()); return point; } }
package com.example.touchpoint; public class LocationPoint { private long _id; private double latitude; private double longitutde; public long get_id() { return _id; } public void set_id(long _id) { this._id = _id; } public double getLatitude() { return latitude; } public void setLatitude(double latitude) { this.latitude = latitude; } public double getLongitutde() { return longitutde; } public void setLongitutde(double longitutde) { this.longitutde = longitutde; } @Override public String toString() { return "LocationPoint [_id=" + _id + ", latitude=" + latitude + ", longitutde=" + longitutde + "]"; } }
package com.example.touchpoint; import java.util.ArrayList; import java.util.List; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.util.Log; import com.amap.mapapi.core.GeoPoint; import com.amap.mapapi.core.OverlayItem; import com.amap.mapapi.map.ItemizedOverlay; import com.amap.mapapi.map.MapView; import com.amap.mapapi.map.Projection; public class RouteItemizedOverlay extends ItemizedOverlay<OverlayItem>{ private List<OverlayItem> list = new ArrayList<OverlayItem>(); public RouteItemizedOverlay(Drawable d) { super(boundCenterBottom(d)); } @Override protected OverlayItem createItem(int id) { return list.get(id); } @Override public int size() { return list.size(); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { Projection proj = mapView.getProjection(); Path path = new Path(); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); for (int i = 0; i < this.size(); i++) { OverlayItem item = (OverlayItem)createItem(i); GeoPoint gp = item.getPoint(); Log.d("aa",gp.getLatitudeE6() +" " + gp.getLongitudeE6()); Point mPoint = new Point(); proj.toPixels(gp, mPoint); if(i == 0) { path.moveTo(mPoint.x, mPoint.y); Log.d("path","[" + mPoint.x + "," + mPoint.y+ "]"); }else{ path.lineTo(mPoint.x, mPoint.y); Log.d("path","[" + mPoint.x + "," + mPoint.y+ "]"); } //canvas.drawText("ca", mPoint.x, mPoint.y, new Paint()); } path.close(); canvas.drawPath(path, paint); super.draw(canvas, mapView, shadow); } public void addMyRoute(List<LocationPoint> points) { for(LocationPoint point : points) { GeoPoint gp = new GeoPoint((int)(point.getLatitude()*1E6),(int)(point.getLongitutde()*1E6)); OverlayItem myItem = new OverlayItem(gp, "title", "snippet"); list.add(myItem); Log.d("add","add an item"); populate(); } } }
package com.example.touchpoint; import java.util.List; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.location.Location; import android.location.LocationListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import com.amap.mapapi.core.GeoPoint; import com.amap.mapapi.location.LocationManagerProxy; import com.amap.mapapi.location.LocationProviderProxy; import com.amap.mapapi.map.MapActivity; import com.amap.mapapi.map.MapController; import com.amap.mapapi.map.MapView; import com.amap.mapapi.map.MyLocationOverlay; import com.amap.mapapi.map.Overlay; import com.amap.mapapi.map.Projection; public class MainActivity extends MapActivity implements LocationListener { private MapView mMapView; private MapController controller; private List<Overlay> mapOverlayList; private LocationManagerProxy locationProxy; private SqliteDAO dao; private RouteItemizedOverlay routeOverlay; private MyOverlay overlay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.main_mapView); mMapView.setBuiltInZoomControls(true); // 开启缩放控件 mMapView.setSatellite(true); // 设置为卫星模式 controller = mMapView.getController(); controller.setZoom(3); mapOverlayList = mMapView.getOverlays(); locationProxy = LocationManagerProxy.getInstance(this); Drawable d = getResources().getDrawable(R.drawable.new_func_dot); routeOverlay = new RouteItemizedOverlay(d); dao = new SqliteDAO(this); } @Override protected void onPause() { locationProxy.removeUpdates(this); dao.close(); super.onPause(); } @Override protected void onResume() { locationProxy.requestLocationUpdates( LocationProviderProxy.MapABCNetwork, 0, 0, this); dao.open(); super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.showroute: showRoutlist(); break; case R.id.closeroute: break; default: break; } return super.onOptionsItemSelected(item); } private void showRoutlist() { mapOverlayList.remove(overlay); locationProxy.removeUpdates(this); // 取消位置监听(数据库不会有更新) routeOverlay.addMyRoute(dao.getAllPoints()); mapOverlayList.add(routeOverlay); } public void closeRoute() { mapOverlayList.clear(); // 清空路线 Location last = locationProxy .getLastKnownLocation(LocationProviderProxy.MapABCNetwork); mapOverlayList.add(new MyOverlay(this, new GeoPoint((int) (last .getLatitude() * 1E6), (int) (last.getLongitude() * 1E6)))); locationProxy.requestLocationUpdates(LocationManagerProxy.GPS_PROVIDER, 0, 0, this); // 开始监听位置变化 } public void onLocationChanged(Location location) { mapOverlayList.clear(); if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); dao.writePositionToDB(latitude, longitude); // 写进数据库 GeoPoint gp = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)); overlay = new MyOverlay(this, gp); mapOverlayList.add(overlay); } } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } } class MyOverlay extends Overlay { private Context mContext; private GeoPoint gp; private Bitmap bitmap; public MyOverlay(Context mContext, GeoPoint gp) { this.mContext = mContext; this.gp = gp; bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.new_func_dot); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { Projection proj = mapView.getProjection(); Point mPoint = proj.toPixels(gp, null); canvas.drawBitmap(bitmap, mPoint.x, mPoint.y, null); super.draw(canvas, mapView, shadow); } }
项目文件在:http://download.csdn.net/detail/czjuttsw/4861690