user1022288
user1022288

Reputation: 45

Android Map Zoom Level for Current Location and Destination

I am displaying the user's current position and their intended destination using Google maps, MappingOverlayActivity and ItemizedOverlay.

What is the best way to firstly set the appropriate zoom level so that both locations are displayed on the map on the screen with the maximum area covered by the map, i.e rather than the current location being in the centre, having the centre being in the middle of the 2 points.. Also when the user's current location changes, how can I then recalulate and reset the appropriate zoom level to incorporate the new current location.

Upvotes: 2

Views: 2137

Answers (2)

skynet
skynet

Reputation: 9908

You use methods from MapController (which you obtain using mapView.getController()).

Specifically animateTo (or moveTo), and zoomToSpan.

Assuming you have two GeoPoints

GeoPoint current;
GeoPoint destination;

do something like this

mapController.zoomToSpan(Math.abs(current.getLatitudeE6() - destination.getLatitudeE6()), Math.abs(current.getLongitudeE6() - destination.getLongitudeE6());
mapController.animateTo(new GeoPoint((current.getLatitudeE6() + destination.getLatitudeE6())/2, (current.getLongitudeE6() + destination.getLongitudeE6())/2));

so you calculate the span (difference) and center (average) of the points.

Upvotes: 3

Sean
Sean

Reputation: 2048

For the center of the coordinates you might need to do some math http://en.wikipedia.org/wiki/Centroid

Upvotes: 0

Related Questions