Android Map Zoom Level for Current Location and De

2019-07-11 04:48发布

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.

2条回答
The star\"
2楼-- · 2019-07-11 05:10

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.

查看更多
在下西门庆
3楼-- · 2019-07-11 05:10

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

查看更多
登录 后发表回答