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;
mStrKey ="输入你的key值";
// 地图控件初始化
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();
}
}
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);
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);
mBMapMan.init( mStrKey , null);
// 地图控件初始化
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();
}
}