Issue
To reduce processor's work to repaint my route on map, I use Path class. And I want to store one Path for one ZoomLevel. I save my Path in SparseArray where key = ZoomLevel and code looks like this
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (shadow){
return;
}
else
if(route==null){
return;
}
drawPath(mapView, canvas);
}
private void drawPath(MapView mv, Canvas canvas) {
Point point = new Point();
if (pathMap.get(gMapView.getZoomLevel())==null){
List<GeoPoint> list = null;
Projection p = mv.getProjection();
List<RouteMachine.Section> routeArray = route.getSections();
p.toPixels(routeArray.get(0).getPoints().get(0), point);
Point rememberThisPoint = new Point(point.x,point.y);
Path path = new Path();
path.moveTo(point.x,point.y);
for (RouteMachine.Section section : routeArray) {
list = section.getPoints();
for (int i=1; i < list.size(); i++) {
p.toPixels(list.get(i), point);
path.lineTo(point.x, point.y);
}
}
pathMap.put(gMapView.getZoomLevel(), path, rememberThisPoint);
}
else{
mv.getProjection().toPixels(route.getSections().get(0).getPoints().get(0), point);
pathMap.offset(gMapView.getZoomLevel(),point);
}
canvas.drawPath(pathMap.get(gMapView.getZoomLevel()),mPaint);
}
At different zoomLevels it works or doesn't work. Level changes, but path looks like path from previous level. I think, it happens becose zoomLevel sometime changes before then map rapaints. And path calculating works between this two actions. ZoomLewel changed, but map hasn't rapainted yet. How can I fix this?
Solution
The problem with testing zoom level using getZoomLevel()
is that after you request a zoom change it starts immediatly returning the new zoom level, but the map image goes through an animation of progressive size change that takes some time to complete.
However, the getProjection().fromPixels()
returns the correct value of projection even during the animation and it can be used to check when animations ends.
I use the following line to test it:
pathInitialLonSpan = projection.fromPixels(0,mapView.getHeight()/2).getLongitudeE6() -
projection.fromPixels(mapView.getWidth(),mapView.getHeight()/2).getLongitudeE6();
It returns the longitude span at map center level. This value is will change during the zoom change animation and will be constant when zomm finishes.
I use it to validate the time when the animation ends and then build the path.
Regards.
Answered By - Luis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.