首先现在地图上定义一个按钮,然后开始进行操作。
LatLng lat = new LatLng(mCurrentLat, mCurrentLon); MapStatus mMapStatus = new MapStatus.Builder() .target(lat) .zoom(19) .build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus); //改变地图状态 mBaiduMap.animateMapStatus(mMapStatusUpdate);
mCurrentLat和mCurrentLon是在自己定义的定位监听函数的实现BDLocationListener的方法中设置的。
/** * 定位SDK监听函数 */ private class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) { return; } mCurrentLat = location.getLatitude(); mCurrentLon = location.getLongitude(); mCurrentAccracy = location.getRadius(); // startBaiduMap(); locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(mCurrentDirection).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); if (mCurrentLat != 4.9E-324 && mCurrentLon != 4.9E-324) { if (isFirstLoc) { navigateTo(location); isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(ll).zoom(19.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); } } } }