Drawing a line/path on Google Maps and see the complete path line in visible area
I have a pa开发者_运维知识库th they draw routes path on the mapview and I search the way for show the complete path with the best zoom level for the longer of the path.
Thanks
If you iterate through the points in your path and find the maximum longitudes and latitudes, (that is, the farthest points north, south, east, and west), you'll have the bounding rectangle for your path (that is, the smallest rectangle that can contain your path).
From there, you can find the center of your rectangle. Get the MapController for your MapView (using MapView.getController()) and use MapController.setCenter() to set the map's center to the center of the rectangle.
Finally, compute the latitude span and longitude span (the north-south size of your rectangle and the east-west size of your rectangle) and pass those into MapController.zoomToSpan().
I know this question had been asked a while back, but if anyone else bumps into the same issue try this answer:
int minLat = Integer.MAX_VALUE; int maxLat = Integer.MIN_VALUE; int minLon = Integer.MAX_VALUE; int maxLon = Integer.MIN_VALUE; for (GeoPoint item : items) { int lat = item.getLatitudeE6(); int lon = item.getLongitudeE6(); maxLat = Math.max(lat, maxLat); minLat = Math.min(lat, minLat); maxLon = Math.max(lon, maxLon); minLon = Math.min(lon, minLon); } mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon)); mapController.animateTo(new GeoPoint( (maxLat + minLat)/2, (maxLon + minLon)/2 ));
It was answered earlier at this StackOverFlow Answer
Hope it helps someone.
精彩评论