百度地图上画出轨迹 for android

用户查看自己的轨迹时候,在手机
端的显示。
1.从服务器把轨迹点拿下来
2.地图上显示点
3.把点连成线
看代码吧,注释比较详细,关键的画线操作就在OverItemT这个类中。
  1. package seu.android007.xin;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.baidu.mapapi.BMapManager;  
  7. import com.baidu.mapapi.GeoPoint;  
  8. import com.baidu.mapapi.ItemizedOverlay;  
  9. import com.baidu.mapapi.LocationListener;  
  10. import com.baidu.mapapi.MapActivity;  
  11. import com.baidu.mapapi.MapView;  
  12. import com.baidu.mapapi.MyLocationOverlay;  
  13. import com.baidu.mapapi.OverlayItem;  
  14. import com.baidu.mapapi.Projection;  
  15. import seu.android007.service.TraceServcie;  
  16. import seu.xin.entity.Trace;  
  17. import android.app.AlertDialog;  
  18. import android.content.Context;  
  19. import android.content.DialogInterface;  
  20. import android.content.Intent;  
  21. import android.content.SharedPreferences;  
  22. import android.graphics.Canvas;  
  23. import android.graphics.Color;  
  24. import android.graphics.Paint;  
  25. import android.graphics.Path;  
  26. import android.graphics.Point;  
  27. import android.graphics.drawable.Drawable;  
  28. import android.location.Location;  
  29. import android.os.Bundle;  
  30. import android.view.View;  
  31. import android.view.ViewGroup.LayoutParams;  
  32. import android.widget.Toast;  
  33.   
  34. public class MyTrace extends MapActivity {  
  35.     static MapView mMapView = null;  
  36.     LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove  
  37.     MyLocationOverlay mLocationOverlay = null// 定位图层  
  38.     static View mPopView = null// 点击mark时弹出的气泡View  
  39.     TraceServcie traceServcie;  
  40.     private ArrayList<Trace> myTraces = new ArrayList<Trace>();  
  41.     SharedPreferences sp;  
  42.   
  43.     @Override  
  44.     public void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.my_trace);  
  47.         traceServcie = new TraceServcie();  
  48.         sp = this.getSharedPreferences("data", MODE_WORLD_READABLE);  
  49.         int uid = sp.getInt("uid"0);  
  50.         myTraces = traceServcie.findTracesByUid(uid);  
  51.   
  52.         BMapApp app = (BMapApp) this.getApplication();  
  53.         if (app.mBMapMan == null) {  
  54.             app.mBMapMan = new BMapManager(getApplication());  
  55.             app.mBMapMan.init(app.mStrKey, new BMapApp.MyGeneralListener());  
  56.         }  
  57.         app.mBMapMan.start();  
  58.         // 如果使用地图SDK,请初始化地图Activity   
  59.         super.initMapActivity(app.mBMapMan);  
  60.   
  61.         mMapView = (MapView) findViewById(R.id.bmapView);  
  62.         mMapView.setBuiltInZoomControls(true);  
  63.         // 设置在缩放动画过程中也显示overlay,默认为不绘制  
  64.         mMapView.setDrawOverlayWhenZooming(true);  
  65.   
  66.         // 添加定位图层   
  67.         mLocationOverlay = new MyLocationOverlay(this, mMapView);  
  68.         mMapView.getOverlays().add(mLocationOverlay);  
  69.   
  70.         // 注册定位事件   
  71.         mLocationListener = new LocationListener() {  
  72.   
  73.             @Override  
  74.             public void onLocationChanged(Location location) {  
  75.                 if (location != null) {  
  76.                     GeoPoint pt = new GeoPoint(  
  77.                             (int) (location.getLatitude() * 1e6),  
  78.                             (int) (location.getLongitude() * 1e6));  
  79.                     mMapView.getController().animateTo(pt);  
  80.                 }  
  81.             }  
  82.         };  
  83.         if (myTraces.size() == 0) {  
  84.             System.out.println("轨迹为空,调转到MyActivityGroup");  
  85.             AlertDialog.Builder builder = new AlertDialog.Builder(MyTrace.this);  
  86.             builder.setTitle("提醒");  
  87.             builder.setMessage("没有轨迹,请添加后再查看");  
  88.             builder.setPositiveButton("确定",  
  89.                     new DialogInterface.OnClickListener() {  
  90.   
  91.                         @Override  
  92.                         public void onClick(DialogInterface dialog, int which) {  
  93.                             // TODO Auto-generated method stub  
  94.                             Intent intent = new Intent();  
  95.                             intent.setClass(MyTrace.this, MyActivityGroup.class);  
  96.                             startActivity(intent);  
  97.                             MyTrace.this.finish();  
  98.                         }  
  99.                     });  
  100.             builder.create().show();  
  101.         } else {  
  102.             System.out.println("获得点后添加到图层");  
  103.             // 添加ItemizedOverlay  
  104.             Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源  
  105.             marker.setBounds(00, marker.getIntrinsicWidth(),  
  106.                     marker.getIntrinsicHeight()); // 为maker定义位置和边界  
  107.   
  108.             mMapView.getOverlays().add(new OverItemT(marker, this, myTraces)); // 添加ItemizedOverlay实例到mMapView  
  109.   
  110.             // 创建点击mark时的弹出泡泡   
  111.             mPopView = super.getLayoutInflater()  
  112.                     .inflate(R.layout.popview, null);  
  113.             mMapView.addView(mPopView, new MapView.LayoutParams(  
  114.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,  
  115.                     MapView.LayoutParams.TOP_LEFT));  
  116.             mPopView.setVisibility(View.GONE);  
  117.         }  
  118.   
  119.     }  
  120.   
  121.     @Override  
  122.     protected void onPause() {  
  123.         BMapApp app = (BMapApp) this.getApplication();  
  124.         app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);  
  125.         mLocationOverlay.disableMyLocation();  
  126.         mLocationOverlay.disableCompass(); // 关闭指南针  
  127.         app.mBMapMan.stop();  
  128.         super.onPause();  
  129.     }  
  130.   
  131.     @Override  
  132.     protected void onResume() {  
  133.         BMapApp app = (BMapApp) this.getApplication();  
  134.         // 注册定位事件,定位后将地图移动到定位点   
  135.         app.mBMapMan.getLocationManager().requestLocationUpdates(  
  136.                 mLocationListener);  
  137.         mLocationOverlay.enableMyLocation();  
  138.         mLocationOverlay.enableCompass(); // 打开指南针  
  139.         app.mBMapMan.start();  
  140.         super.onResume();  
  141.     }  
  142.   
  143.     @Override  
  144.     protected boolean isRouteDisplayed() {  
  145.         // TODO Auto-generated method stub  
  146.         return false;  
  147.     }  
  148.   
  149. }  
  150.   
  151. class OverItemT extends ItemizedOverlay<OverlayItem> {  
  152.   
  153.     private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();  
  154.     private Drawable marker;  
  155.     private Context mContext;  
  156.     private ArrayList<Trace> traces = new ArrayList<Trace>();  
  157.   
  158.     public OverItemT(Drawable marker, Context context, ArrayList<Trace> traces) {  
  159.         super(boundCenterBottom(marker));  
  160.   
  161.         this.marker = marker;  
  162.         this.mContext = context;  
  163.         this.traces = traces;  
  164.         // 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)  
  165.         // 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段  
  166.         for (int i = 0; i < traces.size(); i++) {  
  167.             int lat = new Integer(traces.get(i).getLatitude());  
  168.             int lon = new Integer(traces.get(i).getLongitude());  
  169.             String resName = traces.get(i).getResname();  
  170.             GeoPoint p = new GeoPoint(lat, lon);  
  171.             mGeoList.add(new OverlayItem(p, resName, i + resName));  
  172.             populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法  
  173.         }  
  174.   
  175.     }  
  176.   
  177.     @Override  
  178.     public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
  179.   
  180.         // Projection接口用于屏幕像素坐标和经纬度坐标之间的变换  
  181.         Projection projection = mapView.getProjection();  
  182.         for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList  
  183.             OverlayItem overLayItem = getItem(index); // 得到给定索引的item  
  184.   
  185.             String title = overLayItem.getTitle();  
  186.             // 把经纬度变换到相对于MapView左上角的屏幕像素坐标  
  187.             Point point = projection.toPixels(overLayItem.getPoint(), null);  
  188.   
  189.             // 可在此处添加您的绘制代码   
  190.             Paint paintText = new Paint();  
  191.             paintText.setColor(Color.BLUE);  
  192.             paintText.setTextSize(15);  
  193.             canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本  
  194.         }  
  195.   
  196.         super.draw(canvas, mapView, shadow);  
  197.         // 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素  
  198.         // 先所有点的经度转像素   
  199.         ArrayList<Point> points = new ArrayList<Point>();  
  200.         for (int i = 0; i < this.size(); i++) {  
  201.             Point p = new Point();  
  202.             OverlayItem overLayItem = getItem(i);  
  203.             projection.toPixels(overLayItem.getPoint(), p);  
  204.             points.add(p);  
  205.         }  
  206.         // 第二个画笔 画线   
  207.         Paint paint = new Paint();  
  208.         paint.setColor(Color.BLUE);  
  209.         paint.setDither(true);  
  210.         paint.setStyle(Paint.Style.STROKE);  
  211.         paint.setStrokeJoin(Paint.Join.ROUND);  
  212.         paint.setStrokeCap(Paint.Cap.ROUND);  
  213.         paint.setStrokeWidth(4);  
  214.   
  215.         // 连接 所有点   
  216.         Path path = new Path();  
  217.         path.moveTo(points.get(0).x, points.get(0).y);  
  218.         for (int i = 1; i < points.size(); i++) {  
  219.             path.lineTo(points.get(i).x, points.get(i).y);  
  220.         }  
  221.         // 画出路径   
  222.         canvas.drawPath(path, paint);  
  223.         boundCenterBottom(marker);  
  224.     }  
  225.   
  226.     @Override  
  227.     protected OverlayItem createItem(int i) {  
  228.         // TODO Auto-generated method stub  
  229.         return mGeoList.get(i);  
  230.     }  
  231.   
  232.     @Override  
  233.     public int size() {  
  234.         // TODO Auto-generated method stub  
  235.         return mGeoList.size();  
  236.     }  
  237.   
  238.     @Override  
  239.     // 处理当点击事件   
  240.     protected boolean onTap(int i) {  
  241.         setFocus(mGeoList.get(i));  
  242.         // 更新气泡位置,并使之显示   
  243.         GeoPoint pt = mGeoList.get(i).getPoint();  
  244.         MyTrace.mMapView.updateViewLayout(MyTrace.mPopView,  
  245.                 new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,  
  246.                         LayoutParams.WRAP_CONTENT, pt,  
  247.                         MapView.LayoutParams.BOTTOM_CENTER));  
  248.         MyTrace.mPopView.setVisibility(View.VISIBLE);  
  249.         Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),  
  250.                 Toast.LENGTH_SHORT).show();  
  251.         return true;  
  252.     }  
  253.   
  254.     @Override  
  255.     public boolean onTap(GeoPoint arg0, MapView arg1) {  
  256.         // TODO Auto-generated method stub  
  257.         // 消去弹出的气泡   
  258.         MyTrace.mPopView.setVisibility(View.GONE);  
  259.         return super.onTap(arg0, arg1);  
  260.     }  
  261. }  
package seu.android007.xin;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import seu.android007.service.TraceServcie;
import seu.xin.entity.Trace;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
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.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

public class MyTrace extends MapActivity {
	static MapView mMapView = null;
	LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
	MyLocationOverlay mLocationOverlay = null; // 定位图层
	static View mPopView = null; // 点击mark时弹出的气泡View
	TraceServcie traceServcie;
	private ArrayList<Trace> myTraces = new ArrayList<Trace>();
	SharedPreferences sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_trace);
		traceServcie = new TraceServcie();
		sp = this.getSharedPreferences("data", MODE_WORLD_READABLE);
		int uid = sp.getInt("uid", 0);
		myTraces = traceServcie.findTracesByUid(uid);

		BMapApp app = (BMapApp) this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey, new BMapApp.MyGeneralListener());
		}
		app.mBMapMan.start();
		// 如果使用地图SDK,请初始化地图Activity
		super.initMapActivity(app.mBMapMan);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		// 设置在缩放动画过程中也显示overlay,默认为不绘制
		mMapView.setDrawOverlayWhenZooming(true);

		// 添加定位图层
		mLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMapView.getOverlays().add(mLocationOverlay);

		// 注册定位事件
		mLocationListener = new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
				if (location != null) {
					GeoPoint pt = new GeoPoint(
							(int) (location.getLatitude() * 1e6),
							(int) (location.getLongitude() * 1e6));
					mMapView.getController().animateTo(pt);
				}
			}
		};
		if (myTraces.size() == 0) {
			System.out.println("轨迹为空,调转到MyActivityGroup");
			AlertDialog.Builder builder = new AlertDialog.Builder(MyTrace.this);
			builder.setTitle("提醒");
			builder.setMessage("没有轨迹,请添加后再查看");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Intent intent = new Intent();
							intent.setClass(MyTrace.this, MyActivityGroup.class);
							startActivity(intent);
							MyTrace.this.finish();
						}
					});
			builder.create().show();
		} else {
			System.out.println("获得点后添加到图层");
			// 添加ItemizedOverlay
			Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源
			marker.setBounds(0, 0, marker.getIntrinsicWidth(),
					marker.getIntrinsicHeight()); // 为maker定义位置和边界

			mMapView.getOverlays().add(new OverItemT(marker, this, myTraces)); // 添加ItemizedOverlay实例到mMapView

			// 创建点击mark时的弹出泡泡
			mPopView = super.getLayoutInflater()
					.inflate(R.layout.popview, null);
			mMapView.addView(mPopView, new MapView.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
					MapView.LayoutParams.TOP_LEFT));
			mPopView.setVisibility(View.GONE);
		}

	}

	@Override
	protected void onPause() {
		BMapApp app = (BMapApp) this.getApplication();
		app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
		mLocationOverlay.disableMyLocation();
		mLocationOverlay.disableCompass(); // 关闭指南针
		app.mBMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		BMapApp app = (BMapApp) this.getApplication();
		// 注册定位事件,定位后将地图移动到定位点
		app.mBMapMan.getLocationManager().requestLocationUpdates(
				mLocationListener);
		mLocationOverlay.enableMyLocation();
		mLocationOverlay.enableCompass(); // 打开指南针
		app.mBMapMan.start();
		super.onResume();
	}

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

}

class OverItemT extends ItemizedOverlay<OverlayItem> {

	private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	private Drawable marker;
	private Context mContext;
	private ArrayList<Trace> traces = new ArrayList<Trace>();

	public OverItemT(Drawable marker, Context context, ArrayList<Trace> traces) {
		super(boundCenterBottom(marker));

		this.marker = marker;
		this.mContext = context;
		this.traces = traces;
		// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
		// 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
		for (int i = 0; i < traces.size(); i++) {
			int lat = new Integer(traces.get(i).getLatitude());
			int lon = new Integer(traces.get(i).getLongitude());
			String resName = traces.get(i).getResname();
			GeoPoint p = new GeoPoint(lat, lon);
			mGeoList.add(new OverlayItem(p, resName, i + resName));
			populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
		}

	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
		Projection projection = mapView.getProjection();
		for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
			OverlayItem overLayItem = getItem(index); // 得到给定索引的item

			String title = overLayItem.getTitle();
			// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
			Point point = projection.toPixels(overLayItem.getPoint(), null);

			// 可在此处添加您的绘制代码
			Paint paintText = new Paint();
			paintText.setColor(Color.BLUE);
			paintText.setTextSize(15);
			canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本
		}

		super.draw(canvas, mapView, shadow);
		// 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
		// 先所有点的经度转像素
		ArrayList<Point> points = new ArrayList<Point>();
		for (int i = 0; i < this.size(); i++) {
			Point p = new Point();
			OverlayItem overLayItem = getItem(i);
			projection.toPixels(overLayItem.getPoint(), p);
			points.add(p);
		}
		// 第二个画笔 画线
		Paint paint = new Paint();
		paint.setColor(Color.BLUE);
		paint.setDither(true);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(4);

		// 连接 所有点
		Path path = new Path();
		path.moveTo(points.get(0).x, points.get(0).y);
		for (int i = 1; i < points.size(); i++) {
			path.lineTo(points.get(i).x, points.get(i).y);
		}
		// 画出路径
		canvas.drawPath(path, paint);
		boundCenterBottom(marker);
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mGeoList.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mGeoList.size();
	}

	@Override
	// 处理当点击事件
	protected boolean onTap(int i) {
		setFocus(mGeoList.get(i));
		// 更新气泡位置,并使之显示
		GeoPoint pt = mGeoList.get(i).getPoint();
		MyTrace.mMapView.updateViewLayout(MyTrace.mPopView,
				new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT, pt,
						MapView.LayoutParams.BOTTOM_CENTER));
		MyTrace.mPopView.setVisibility(View.VISIBLE);
		Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),
				Toast.LENGTH_SHORT).show();
		return true;
	}

	@Override
	public boolean onTap(GeoPoint arg0, MapView arg1) {
		// TODO Auto-generated method stub
		// 消去弹出的气泡
		MyTrace.mPopView.setVisibility(View.GONE);
		return super.onTap(arg0, arg1);
	}
}

运行的效果图:


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值