android 百度地图轨迹回放


android百度地图实现轨迹回放,就需要用PolylineOptions画线作为轨迹效果,和添加覆盖物移动就可以了

android 百度地图轨迹回放_第1张图片

android 百度地图轨迹回放_第2张图片

android 百度地图轨迹回放_第3张图片

package baidumapsdk.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.DotOptions;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;

/**
 * 此demo用来展示如何在地图上用GraphicsOverlay添加点、线、多边形、圆 同时展示如何在地图上用TextOverlay添加文字
 * 
 */
public class GeometryDemo extends Activity {

	// 地图相关
	MapView mMapView;
	BaiduMap mBaiduMap;
	// UI相关
	Button resetBtn;
	Button clearBtn;
	BitmapDescriptor bdA = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_marka);
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_geometry);
		// 初始化地图
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		// UI初始化
		clearBtn = (Button) findViewById(R.id.button1);
		resetBtn = (Button) findViewById(R.id.button2);

		OnClickListener clearListener = new OnClickListener() {
			public void onClick(View v) {
				clearClick();
			}
		};
		OnClickListener restListener = new OnClickListener() {
			public void onClick(View v) {
				//resetClick();
				Refresh();
			}
		};

		clearBtn.setOnClickListener(clearListener);
		resetBtn.setOnClickListener(restListener);

		// 界面加载时添加绘制图层
		addCustomElementsDemo();
	}
	
	
	List points =null;
	Polyline mMarkerPolyLine=null;
	Marker mMarkerA;
	public void Refresh(){
		
	    final Handler handler = new Handler(){  	
	    	double x = 0.02;
   	        public void handleMessage(Message msg) {                      	     	        	
	    		
	    		LatLng p1 = new LatLng(39.97923, 116.357428+x);
   		    	points.add(p1);
   			    mMarkerPolyLine.setPoints(points);
   			    mMarkerA.setPosition(p1);
   			    x=x+0.02;
   	        }    
       };  
       
	   TimerTask task = new TimerTask(){  
		       public void run() {  
		       Message message = new Message();      
		       message.what = 1;      
		       handler.sendMessage(message);    
		    }  
       }; 
       
       Timer timer = new Timer(true);
       timer.schedule(task,1000, 1500); 
	}

	
	/**
	 * 添加点、线、多边形、圆、文字  aj
	 */
	public void addCustomElementsDemo() {
		// 添加折线
		LatLng p1 = new LatLng(39.97923, 116.357428);
		LatLng p2 = new LatLng(39.94923, 116.397428);
		/*	LatLng p3 = new LatLng(39.97923, 116.437428);
		LatLng p4 = new LatLng(39.95923, 116.467428);
		LatLng p5 = new LatLng(39.96923, 116.487428);*/
		
		points = new ArrayList();
		points.add(p1);
		points.add(p2);
		/*	points.add(p3);
		points.add(p4);
		points.add(p5);*/
	

		
		OverlayOptions ooPolyline = new PolylineOptions().width(12)
				.color(0xAAFF0000).points(points);
		
		 mMarkerPolyLine = (Polyline) mBaiduMap.addOverlay(ooPolyline);	
		 
			OverlayOptions ooA = new MarkerOptions().position(p2).icon(bdA);
			mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
	}

	
	public void resetClick() {
		// 添加绘制元素
		addCustomElementsDemo();
	}

	public void clearClick() {
		// 清除所有图层
		mMapView.getMap().clear();
	}

	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		mMapView.onDestroy();
		super.onDestroy();
	}

}



你可能感兴趣的:(android)