android 运用百度地图画轨迹

package app.com;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.os.Bundle;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;


public class AppActivity extends MapActivity {
    private BMapManager mBMapMan;
    private MapView mapView;
    private String mStrKey;
    private GeoPoint gPoint1,gPoint2,gPoint3;//连线的点
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mStrKey ="输入你的key值";
        mBMapMan=new BMapManager(AppActivity.this);
        mBMapMan.init( mStrKey , null);
        super.initMapActivity(mBMapMan);

        // 地图控件初始化
        mapView=(MapView)findViewById(R.id.bmapsView);
        mapView.setBuiltInZoomControls(true);
        mapView.setDrawOverlayWhenZooming(true);
        
        MyOverlay myOverlay=new MyOverlay();
        mapView.getOverlays().add(myOverlay);
        
        MapController mapController=mapView.getController();  
        mapController.zoomIn();
        
        // 定义一个经纬度
        gPoint1 = new GeoPoint((int) (39.915*1E6),(int) (116.404*1E6));
        gPoint2 = new GeoPoint((int) (39.868935*1E6),(int) (116.39078*1E6));
        gPoint3 = new GeoPoint((int) (39.85963*1E6),(int) (116.44194*1E6));
        mapController.animateTo(gPoint1);

    }

    
    class MyOverlay extends Overlay
    {
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
        Projection projection=mapView.getProjection();
        Point p1=new Point();
        Point p2=new Point();
        Point p3=new Point();
        
        //经度转像素
        projection.toPixels(gPoint1, p1);
        projection.toPixels(gPoint2, p2);
        projection.toPixels(gPoint3, p3);
        
        //第一个画笔   画圆
        Paint fillPaint=new Paint();
        fillPaint.setColor(Color.BLUE);
        fillPaint.setAntiAlias(true);
        fillPaint.setStyle(Style.FILL);
        
        //将图画到上层
        canvas.drawCircle(p1.x, p1.y, 5.0f, fillPaint);
        canvas.drawCircle(p2.x, p2.y, 5.0f, fillPaint);
        canvas.drawCircle(p3.x, p3.y, 5.0f, fillPaint);
        
        //第二个画笔  画线
        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(p1.x, p1.y);
        path.lineTo(p2.x, p2.y);
        path.lineTo(p3.x, p3.y);
        
        //画出路径
        canvas.drawPath(path, paint);
    }
    }

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

    @Override
    protected void onDestroy() {
    if (mBMapMan!=null) {
        mBMapMan.destroy();
        mBMapMan=null;
    }
    super.onDestroy();
    }

    @Override
    protected void onPause() {
    if (mBMapMan!=null) {
        mBMapMan.stop();
    }
    super.onPause();
    }

    @Override
    protected void onResume() {
    if (mBMapMan!=null) {
        mBMapMan.start();
    }
    super.onResume();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值