public class YiYuanMapActivity extends BaseActivity{ private ArrayList<LatitudeModel> latitudeList; // 定位相关 LocationClient mLocClient; private MyLocationConfiguration.LocationMode mCurrentMode; public MyLocationListenner myListener = new MyLocationListenner(); boolean isFirstLoc = true; // 是否首次定位 /** * MapView 是地图主控件 */ private MapView mMapView; private BaiduMap mBaiduMap; // 初始化全局 bitmap 信息,不用时及时 recycle BitmapDescriptor bd = BitmapDescriptorFactory .fromResource(R.drawable.icon_gcoding); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { SDKInitializer.initialize(getApplicationContext()); } catch (Exception e) { // TODO: handle exception ToastUtils.shortToast(getApplicationContext(), "地图api初始化失败"); return; } setContentView(R.layout.activity_yi_yuan_map); mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL; mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f); mBaiduMap.setMapStatus(msu); BitmapDescriptor centerBitmap = BitmapDescriptorFactory .fromResource(R.drawable.icon_geo); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(getApplicationContext()); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000); option.setIsNeedAddress(true); mLocClient.setLocOption(option); mLocClient.start(); mBaiduMap .setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, centerBitmap, 0, 0)); initOverlay(); } /** * 定位SDK监听函数 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(0).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(12); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } } public void onReceivePoi(BDLocation poiLocation) { } } private void initOverlay() { // add marker overlay for (int i = 0; i < latitudeList.size(); i++) { String ycode = latitudeList.get(i).ycode;//weidu String xcode = latitudeList.get(i).xcode; double lat = Double.parseDouble(ycode); double lng = Double.parseDouble(xcode); LatLng ll = new LatLng(lat, lng); MarkerOptions oo = new MarkerOptions().position(ll).icon(bd) .zIndex(9).draggable(true); // 掉下动画 oo.animateType(MarkerOptions.MarkerAnimateType.drop); Marker mMarkerA = (Marker) (mBaiduMap.addOverlay(oo)); mMarkerA = null; } mBaiduMap.setOnMarkerDragListener(new BaiduMap.OnMarkerDragListener() { public void onMarkerDrag(Marker marker) { } public void onMarkerDragEnd(Marker marker) { } public void onMarkerDragStart(Marker marker) { } }); } /** * 清除所有Overlay * * @param view */ public void clearOverlay(View view) { mBaiduMap.clear(); } /** * 重新添加Overlay * * @param view */ public void resetOverlay(View view) { clearOverlay(null); initOverlay(); } @Override protected void onDestroy() { // MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy() mMapView.onDestroy(); super.onDestroy(); // 回收 bitmap 资源 bd.recycle(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } }![]()
百度地图4.0多个经纬度展示在地图上
最新推荐文章于 2024-06-21 09:29:04 发布