设置缩放比例
1,设置缩放比例的api是
aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
如果你直接设置是没用的,因为此时地图还没加载成功。所以要监听地图加载成功的事件
aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
@Override
public void onMapLoaded() {
aMap.moveCamera(CameraUpdateFactory.zoomTo(18));
}
});
导航算路成功后点击路线切换
点击回调的polyline id跟导航id是没有任何关联的,只能通过判断整条线路所有点是否包含polyline的点来判断用户点击是哪条线段。这里取得点是polyline的中间点作参考点
map.setOnPolylineClickListener(polyline -> onClickPolyline(polyline));
private void onClickPolyline(Polyline polyline) {
//突出选择的那条路
if (routeOverlays != null && routeOverlays.size() == 1) {
return;
}
List<LatLng> polylinePoints = polyline.getPoints();
LatLng polylinePoint = polylinePoints.get(polylinePoints.size() / 2);
NaviLatLng naviLatLng = new NaviLatLng(polylinePoint.latitude, polylinePoint.longitude);
for (int i = 0; i < routeOverlays.size(); i++) {
RouteOverLay routeOverLay = routeOverlays.valueAt(i);
List<NaviLatLng> coordList = routeOverLay.getAMapNaviPath().getCoordList();
if (coordList.contains(naviLatLng)) {
routeIndex = i;
routeOverLay.setTransparency(1.0f);
routeOverLay.setZindex(1);
aMapNavi.selectRouteId(routeOverlays.keyAt(i));
updateFlexibleView(aMapNavi.getNaviPath());
Log.i(TAG, "onPolylineClick: true: " + i);
} else {
routeOverLay.setTransparency(0.4f);
routeOverLay.setZindex(0);
Log.i(TAG, "onPolylineClick: false: " + i);
}
}
}
将算路的路径在限定区域显示
/**
* 移动镜头到当前的视角。
*
* @since V2.1.0
*/
public void zoomToSpan() {
if (startPoint != null) {
if (mAMap == null) {
return;
}
try {
LatLngBounds bounds = getLatLngBounds();
// LatLng latLng = new LatLng(((startPoint.latitude + endPoint.latitude) / 2), ((startPoint.longitude + endPoint.longitude) / 2 - 0.2));
mAMap.animateCamera(CameraUpdateFactory
.newLatLngBoundsRect(bounds, Utils.dp2px(550), Utils.dp2px(50), Utils.dp2px(50), Utils.dp2px(50)));
} catch (Throwable e) {
e.printStackTrace();
}
}
}
protected LatLngBounds getLatLngBounds() {
LatLngBounds.Builder b = LatLngBounds.builder();
b.include(new LatLng(startPoint.latitude, startPoint.longitude));
b.include(new LatLng(endPoint.latitude, endPoint.longitude));
return b.build();
}
amap切换到后台大概3分钟后在进入显示白屏的情况
分析:多半是因为onResume()后做了其他操作,比如挪动中心点等地图操作。大概原因是此时地图正在绘制,还未加载完毕。此刻挪动中心点就会出现白屏问题
@Override
public void onResume() {
super.onResume();
mapView.onResume();
if (currentLocation != null) {
Log.i(TAG, "onResume: currentlocation not null");
map.clear();
moveToPosition(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
}
}
解决方式:在地图加载完成后挪动中心点
map.addOnMapLoadedListener(() -> { moveToPosition(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));});