我的部分应用程序使用带有CardView元素的RecyclerView,每个CardView元素都包含绘制特定路径的Google Map。在每张地图上我都在缩放并在地图中定位路线,这样它就可以填满整个视图,但是当我开始滚动时问题就会到来,而且每一个下一步的CardView都没有将我的路线定位在地图中,但是它仍然没有定位和未定位。图片如下:
我使用onBindViewHolder来设置地图和路线图,然后我使用zoomToLatLng来缩放地图
@Override
public void onBindViewHolder(RouteViewHolder holder, int position) {
holder.destination.setText(routesCV.get(position).getDestination());
holder.startingPoint.setText(routesCV.get(position).getStartingPoint());
GoogleMap gMap = holder.map.getMap();
if (gMap != null) {
ArrayList<PointModel> mapPosition = routesCV.get(position).getPoints();
ArrayList<LatLng> points = new ArrayList<LatLng>();
for (PointModel item : mapPosition) {
points.add(new LatLng(item.getLatitude(),item.getLongitude()));
}
for (LatLng item : points) {
gMap.addMarker(new MarkerOptions().position(item));
}
zoomMapToLatLngBounds(holder.linearlayout, gMap, points);
}
}
缩放方法:
private void zoomMapToLatLngBounds(final LinearLayout layout,final GoogleMap mMap, final ArrayList<LatLng> bounds){
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (int i=0; i<bounds.size(); i++) {
boundsBuilder.include(bounds.get(i));
}
final LatLngBounds boundsfinal = boundsBuilder.build();
final ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(boundsfinal, 25));
}
});
来自日志public void onGlobalLayout()甚至不执行。你知道可能是什么问题吗? TNX!
答案 0 :(得分:0)
我制作了一个OnMapLoadedCallback()
函数来定位地图。它现在有效!