Android GIS osmdroid地图使用
package com.example.administrator.test; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.GSCAndroid.DBSqlite.SqliteHelper; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.model.LatLng; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.osmdroid.ResourceProxy; import org.osmdroid.util.GeoPoint; import org.osmdroid.views.overlay.ItemizedIconOverlay; import org.osmdroid.views.overlay.ItemizedOverlayWithFocus; import org.osmdroid.views.overlay.OverlayItem; import org.osmdroid.views.overlay.PathOverlay; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { public MapView mMapView ; public static boolean ViewChanges = false; public static SqliteHelper sqliteHelper = null; public static Bitmap pallet; Paint paint; private ResourceProxy mResourceProxy; private ItemizedOverlayWithFocus<OverlayItem> mLocationOverlay; private PathOverlay mLine; private float mLevel; private ArrayList<OverlayItem> mItems = new ArrayList<OverlayItem>(); private BaiduMap mBaiduMap; private BitmapDescriptor mCurrentMarker; private MyLocationConfiguration.LocationMode mCurrentMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); //获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); //地图出不来可能是经纬度写反了 LatLng cenpt = new LatLng(30.508923,114.406027); MapStatus mMapStatus = new MapStatus.Builder() .target(cenpt) .zoom(18) .build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //改变地图状态 mBaiduMap.setMapStatus(mMapStatusUpdate); //开启交通图 //mBaiduMap.setTrafficEnabled(true); // ////普通地图 // mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); // ////卫星地图 // mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); // ////空白地图, 基础地图瓦片将不会被渲染。在地图类型中设置为NONE,将不会使用流量下载基础地图瓦片图层。使用场景:与瓦片图层一起使用,节省流量,提升自定义瓦片图下载速度。 // mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE); // 以Sticky的形式注册 EventBus.getDefault().register(this); // mLevel = 0f; // mResourceProxy = new DefaultResourceProxyImpl(this); // mapView = new MapView(this); //constructor // mapView.setMultiTouchControls(true); // mapView.setClickable(false); // mapView.setBuiltInZoomControls(true); // setContentView(mapView); // mapView.setTileSource(TileSourceFactory.MAPNIK); // mapView.getController().setZoom(13); // mapView.getController().setCenter(new GeoPoint(114.406027,30.508923)); // mapView.setUseDataConnection(true); // mapView.setMaxZoomLevel(22); // GeoPoint center = new GeoPoint(114.406027,30.508923); // initData(center,0); //initData(); //地图改变监听 // mapView.setMapListener(new MapListener() { // @Override // public boolean onScroll(ScrollEvent event) { // return false; // } // // @Override // public boolean onZoom(ZoomEvent event) { // // mapView.getOverlays().clear(); // init(); // return true; // } // // @Override // public boolean onTouch(TouchEvent event) { // return false; // } // }); } @Subscribe(threadMode = ThreadMode.MAIN,sticky = true) public void onEventMainThread(SendGPSData gpsData) { //Toast.makeText(this, "点的数量" + gpsData.Heading, Toast.LENGTH_SHORT).show(); // if(gpsData!=null){ // GeoPoint point = new GeoPoint(gpsData.Latitude, gpsData.Longitude); // // initData(point, (float) gpsData.Heading); // } } private void initData() { //private void initData(GeoPoint gpsPoint,float heading) { mLine = new PathOverlay(Color.BLUE, this); //********************外界传过来点的坐标(需要组合代码的)******************** GeoPoint center = new GeoPoint(114.406027,30.508923); GeoPoint center1 = new GeoPoint(114.416027,30.508923); GeoPoint center2 = new GeoPoint(114.426027,30.508923); AddPoint(center); AddPoint(center1); AddPoint(center2); //********************外界传过来点的坐标(结束)******************** //mapView.getOverlays().add(mLine); OverlayItem overlayItem = mItems.get(mItems.size() - 1); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhinanzhen); bitmap = rotateBitmap(bitmap, 30f); Bitmap smallBitmap = small(bitmap, 0.3f); Drawable drawable = new BitmapDrawable(smallBitmap); overlayItem.setMarker(drawable); //Toast.makeText(this, "点的数量" + mItems.size(), Toast.LENGTH_SHORT).show(); } //旋转图片 private Bitmap rotateBitmap(Bitmap origin, float alpha) { if (origin == null) { return null; } int w = origin.getWidth(); int h = origin.getHeight(); Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图 Canvas cv = new Canvas(newb); Matrix m = new Matrix(); // m.postScale(1, -1); //镜像垂直翻转 // m.postScale(-1, 1); //镜像水平翻转 m.postRotate(-90); //旋转-90度 Bitmap new2 = Bitmap.createBitmap(origin, 0, 0, w, h, m, true); cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()), new Rect(0, 0, w, h), null); return newb; } //添加点 public void AddPoint(GeoPoint center) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.timg); // if (mapView.getZoomLevel() == 0) { // mLevel = 0.8f; // } else { // mLevel = (float) mapView.getMaxZoomLevel() / mapView.getZoomLevel(); // } Bitmap smallBitmap = small(bitmap, mLevel); Drawable drawable = new BitmapDrawable(smallBitmap); OverlayItem item = new OverlayItem("~title~", "I`m a marker," + center.getLatitude(), center); item.setMarker(drawable); mItems.add(item); this.mLocationOverlay = new ItemizedOverlayWithFocus<OverlayItem>( mItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() { @Override public boolean onItemSingleTapUp(final int index, final OverlayItem item) { return true; } @Override public boolean onItemLongPress(final int index, final OverlayItem item) { return false; } }, mResourceProxy); // this.mLocationOverlay.setFocusItemsOnTap(true); // this.mLocationOverlay.setFocusedItem(0); mLine.addPoint(center); //mapView.getOverlays().add(mLocationOverlay); } /** * Bitmap等比缩放的方法 */ private static Bitmap small(Bitmap bitmap, float num) { Matrix matrix = new Matrix(); matrix.postScale(num, num); //长和宽放大缩小的比例 Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizeBmp; } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } }