Android Map Zoom Level for Current Location and Destination

独自空忆成欢 提交于 2019-12-11 02:31:47

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/8380403/android-map-zoom-level-for-current-location-and-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!