初始化百度地图:
1 private void initViews() {2 4 mMapView = (MapView) findViewById(R.id.bmapView);5 mBaiduMap = mMapView.getMap();6 // 初始化地图范围级别7 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);8 mBaiduMap.setMapStatus(msu);9 }
--------------------华丽分割线-----------------------
// 初始化bitmap 信息,不用时及时 recycleBitmapDescriptor friend_1;
初始化剪裁的图片(此处为生成圆形图片):
1 friend_1 = BitmapDescriptorFactory.fromBitmap(2 BMapUtil.createCircleImage(BitmapFactory.decodeResource(getResources(), R.drawable.friend_1), 90));
圆形图片生成方法:
1 /** 2 * 根据原图和变长绘制圆形图片 3 * @param source 4 * @param min 5 * @return 6 */ 7 public static Bitmap createCircleImage(Bitmap source, int radius) { 8 Bitmap scaledSrcBmp; 9 int diameter = radius * 2;10 // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片11 int bmpWidth = source.getWidth();12 int bmpHeight = source.getHeight();13 int squareWidth = 0, squareHeight = 0;14 int x = 0, y = 0;15 Bitmap squareBitmap;16 if (bmpHeight > bmpWidth) { // 高大于宽17 squareWidth = squareHeight = bmpWidth;18 x = 0;19 y = (bmpHeight - bmpWidth) / 2;20 // 截取正方形图片21 squareBitmap = Bitmap.createBitmap(source, x, y, squareWidth,22 squareHeight);23 } else if (bmpHeight < bmpWidth) { // 宽大于高24 squareWidth = squareHeight = bmpHeight;25 x = (bmpWidth - bmpHeight) / 2;26 y = 0;27 squareBitmap = Bitmap.createBitmap(source, x, y, squareWidth,28 squareHeight);29 } else {30 squareBitmap = source;31 }32 if (squareBitmap.getWidth() != diameter33 || squareBitmap.getHeight() != diameter) {34 scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,35 diameter, true);36 } else {37 scaledSrcBmp = squareBitmap;38 }39 Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),40 scaledSrcBmp.getHeight(), Config.ARGB_8888);41 Canvas canvas = new Canvas(output);42 43 Paint paint = new Paint();44 Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),45 scaledSrcBmp.getHeight());46 47 paint.setAntiAlias(true);48 paint.setFilterBitmap(true);49 paint.setDither(true);50 canvas.drawARGB(0, 0, 0, 0);51 canvas.drawCircle(scaledSrcBmp.getWidth() / 2,52 scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,53 paint);54 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));55 canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);56 bmp = null;57 squareBitmap = null;58 scaledSrcBmp = null;59 return output;60 }
初始化位置信息:
1 LatLng position_1 = new LatLng(39.963175, 116.400244);
初始化OverlayOptions:
1 OverlayOptions ooA = new MarkerOptions().position(position_1).icon(friend_1)2 .zIndex(9).draggable(true);
再通过addOverlay添加到地图:
1 mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
注册setOnMarkerClickListener:
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() { public boolean onMarkerClick(final Marker marker) { // todo 判断mMarkerA 的点击事件 }}
当退出时,释放资源:
@Override protected void onPause() { // MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause() mMapView.onPause(); super.onPause(); } @Override protected void onResume() { // MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume() mMapView.onResume(); super.onResume(); } @Override protected void onDestroy() { // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy() mMapView.onDestroy(); super.onDestroy(); // 回收 bitmap 资源 friend_1.recycle(); }