百度地图多个Marker点自适应屏幕

多个Marker点同时展示在可视范围内,并自动缩放。
Marker点和路径规划轨迹同时在可视范围内,并自动缩放。
在xml中就一个mapview,如下:





前置需要集成百度地图部分功能。
Activity如下: 【百度地图多个Marker点自适应屏幕】
public class MainActivity extends AppCompatActivity implements OnGetRoutePlanResultListener {private MapViewmMapView; private BaiduMap mBaiduMap; private RoutePlanSearch mSearch; private DrivingRouteLineroute= null; private DrivingRouteOverlay overlay = null; private BitmapDescriptor mIconMaker; private List latList = new ArrayList<>(); // 天安门坐标 private double mLat1 = 39.915291; private double mLon1 = 116.403857; // 百度大厦坐标 private double mLat2 = 40.056858; private double mLon2 = 116.308194; // 西湖坐标 private double mLat3 = 30.2446230000; private double mLon3 = 120.1517040000; private LatLng loc_start; private LatLng loc_end; private LatLng loc_position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); initView(); initData(); }private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); mIconMaker = BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_round); //创建驾车线路规划检索实例 mSearch = RoutePlanSearch.newInstance(); mSearch.setOnGetRoutePlanResultListener(this); }private void initData() { loc_start = new LatLng(mLat1, mLon1); loc_end = new LatLng(mLat2, mLon2); loc_position = new LatLng(mLat3, mLon3); latList.add(loc_start); latList.add(loc_end); latList.add(loc_position); //多个Maker展示在同一可视范围内 //showMarkers(); //规划路线和Maker展示在同一可视范围内 search(); }private void showMarkers() { for (LatLng position : latList) { addMarker(position); } updateMap(); }private void search() {PlanNode stNode = PlanNode.withLocation(latList.get(0)); PlanNode enNode = PlanNode.withLocation(latList.get(1)); mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(enNode)); }@Override public void onGetDrivingRouteResult(DrivingRouteResult result) { //获取驾车线路规划结果 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show(); } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { //起终点或途经点地址有岐义,通过以下接口获取建议查询信息 //result.getSuggestAddrInfo() return; } //获取地理编码结果 if (result.error == SearchResult.ERRORNO.NO_ERROR) { route = result.getRouteLines().get(0); overlay = new DrivingRouteOverlay(mBaiduMap); overlay.setData(route); overlay.addToMap(); //overlay.zoomToSpan(); } //添加Marker点 addMarker(latList.get(2)); //把路径规划的所有取到的点添加到自己创建的list中

for (int j = 0; j < result.getRouteLines().get(0).getAllStep().size(); j++) { for (LatLng l : result.getRouteLines().get(0).getAllStep().get(j).getWayPoints()) { latLngList.add(l); } }

updateMap() ; } private void addMarker(LatLng position) { OverlayOptions overlayOptions = new MarkerOptions().position(position).icon( mIconMaker) ; mBaiduMap.addOverlay(overlayOptions) ; } private void updateMap() { //路径规划和Marker点同时展示时注释掉,仅多个Marker点时打开 // mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() { // @Override // public void onMapLoaded() { //仿照zoomToSpan()写的自适应 LatLngBounds.Builder builder = new LatLngBounds.Builder() ; for ( int i = 0 ; i < latList.size() ; i++) { builder.include( latList.get(i)) ; } //更新地图 mBaiduMap.animateMapStatus(MapStatusUpdateFactory . newLatLngBounds(builder.build())) ; // } // }); } @Override public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) { } @Override public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) { } @Override public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) { } @Override public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) { } @Override public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) { }}


下载地址:http://download.csdn.net/detail/qq_19666821/9814223

    推荐阅读