<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.baidu.mapapi.MapView android:id="@+id/bmapsView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
1 package com.xmb.navigationdemoactivity; 2 3 import android.content.Context; 4 5 import android.graphics.Canvas; 6 7 import android.graphics.Color; 8 9 import android.graphics.Paint; 10 11 import android.graphics.Paint.Style; 12 13 import android.graphics.Path; 14 15 import android.graphics.Point; 16 17 import android.os.Bundle; 18 import android.util.Log; 19 import android.widget.Toast; 20 21 import com.baidu.mapapi.BMapManager; 22 import com.baidu.mapapi.MKEvent; 23 import com.baidu.mapapi.MKGeneralListener; 24 25 import com.baidu.mapapi.GeoPoint; 26 27 import com.baidu.mapapi.MapActivity; 28 29 import com.baidu.mapapi.MapController; 30 31 import com.baidu.mapapi.MapView; 32 33 import com.baidu.mapapi.Overlay; 34 35 import com.baidu.mapapi.Projection; 36 37 38 39 public class LineTest extends MapActivity { 40 41 private Context mContext; 42 43 private MapView mapView; 44 private BMapManager mMapManager = null; 45 private double mLat1 = 23.178848; // point1緯度 46 private double mLon1 = 113.418792; // point1經度 47 48 private double mLat2 = 23.078848; 49 private double mLon2 = 113.418792; 50 51 private double mLat3 = 23.178848; 52 private double mLon3 = 113.018792; 53 @Override 54 55 protected boolean isRouteDisplayed() { 56 57 // TODO Auto-generated method stub 58 59 return false; 60 61 } 62 63 private GeoPoint gpoint1, gpoint2, gpoint3;// 連線的點 64 65 @Override 66 67 protected void onCreate(Bundle arg0) { 68 69 super.onCreate(arg0); 70 71 setContentView(R.layout.line); 72 mMapManager = new BMapManager(getApplication()); 73 mMapManager.init("C9DF0F24E9FFCCEB4AF1DA296E8A694B311A3073", new MyGeneralListener()); 74 75 super.initMapActivity(mMapManager); 76 77 78 /* BaseApplication baseApp = (BaseApplication) this.getApplication(); 79 80 if (baseApp.mMapManagerage == null) { 81 82 baseApp.mMapManagerage = new BMapManager(mContext); 83 84 baseApp.mMapManagerage.init(baseApp.mStrKey, 85 86 new BaseApplication.MyGeneralListener()); 87 88 } 89 90 baseApp.mMapManagerage.start(); 91 92 super.initMapActivity(baseApp.mMapManagerage);// 初始化map sdk 93 94 */ mapView = (MapView) findViewById(R.id.bmapsView1); 95 96 mapView.setBuiltInZoomControls(true); 97 98 // 設置在縮放動畫過程中也顯示overlay,默認為不繪制 99 100 mapView.setDrawOverlayWhenZooming(true); 101 102 103 104 // RouteLine routeLine = 105 106 // (RouteLine)getIntent().getSerializableExtra("routeLine"); 107 gpoint1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6)); 108 109 gpoint2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6)); 110 111 gpoint3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6)); 112 MapController mapController = mapView.getController(); 113 //mapController.animateTo(gpoint1);//設置一個起點 114 //這里畫點和連接線 115 mapController.setCenter(gpoint1); 116 MyOverlay myOverlay = new MyOverlay(); 117 118 mapView.getOverlays().add(myOverlay); 119 120 121 122 123 124 mapController.zoomIn(); 125 126 127 128 129 130 131 132 } 133 134 135 // 常用事件監聽,用來處理通常的網絡錯誤,授權驗證錯誤等 136 class MyGeneralListener implements MKGeneralListener { 137 @Override 138 public void onGetNetworkState(int iError) { 139 Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError); 140 Toast.makeText(LineTest.this, "您的網絡出錯啦!", 141 Toast.LENGTH_LONG).show(); 142 } 143 144 @Override 145 public void onGetPermissionState(int iError) { 146 Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError); 147 if (iError == MKEvent.ERROR_PERMISSION_DENIED) { 148 // 授權Key錯誤: 149 Toast.makeText(LineTest.this, 150 "請在BMapApiDemoApp.java文件輸入正確的授權Key!", 151 Toast.LENGTH_LONG).show(); 152 } 153 } 154 } 155 156 157 158 @Override 159 protected void onDestroy() { 160 // TODO Auto-generated method stub 161 if (mMapManager != null) { 162 mMapManager.destroy(); 163 mMapManager = null; 164 } 165 super.onDestroy(); 166 } 167 168 169 170 @Override 171 protected void onPause() { 172 // TODO Auto-generated method stub 173 if (mMapManager != null) { 174 mMapManager.stop(); 175 } 176 super.onPause(); 177 } 178 179 180 181 @Override 182 protected void onResume() { 183 // TODO Auto-generated method stub 184 if (mMapManager != null) { 185 mMapManager.start(); 186 } 187 super.onResume(); 188 } 189 190 191 class MyOverlay extends Overlay { 192 193 194 195 @Override 196 197 public void draw(Canvas canvas, MapView mapView, boolean shadow) { 198 199 super.draw(canvas, mapView, shadow); 200 201 202 203 Projection projection = mapView.getProjection(); 204 205 Point p1 = new Point(); 206 207 Point p2 = new Point(); 208 209 Point p3 = new Point(); 210 211 // 經度轉像素 212 213 projection.toPixels(gpoint1, p1); 214 215 projection.toPixels(gpoint2, p2); 216 217 projection.toPixels(gpoint3, p3); 218 219 220 221 //第一個畫筆 畫圓 222 223 Paint fillPaint = new Paint(); 224 225 fillPaint.setColor(Color.GREEN); 226 227 fillPaint.setAntiAlias(true); 228 229 fillPaint.setStyle(Style.FILL); 230 231 232 233 // 將圖畫到上層 234 235 canvas.drawCircle(p1.x, p1.y, 5.0f, fillPaint); 236 237 canvas.drawCircle(p2.x, p2.y, 5.0f, fillPaint); 238 239 canvas.drawCircle(p3.x, p3.y, 5.0f, fillPaint); 240 241 242 243 //第二個畫筆 畫線 244 245 Paint paint = new Paint(); 246 247 paint.setColor(Color.BLUE); 248 249 paint.setDither(true); 250 251 paint.setStyle(Paint.Style.STROKE); 252 253 paint.setStrokeJoin(Paint.Join.ROUND); 254 255 paint.setStrokeCap(Paint.Cap.ROUND); 256 257 paint.setStrokeWidth(8); 258 259 260 261 //連接 262 263 Path path = new Path(); 264 265 path.moveTo(p1.x, p1.y); 266 267 path.lineTo(p2.x, p2.y); 268 269 path.lineTo(p3.x, p3.y); 270 271 //畫出路徑 272 273 canvas.drawPath(path, paint); 274 275 } 276 277 } 278 279 280 281 }