我希望以英里为单位设置Google地图的缩放级别, 现在,我正在使用此代码,但我认为这是错误的,需要改进。
map.animateCamera(CameraUpdateFactory.newLatLngZoom(MYLOC, calculateZoomLevel()));
public byte calculateZoomLevel() {
byte zoom = 1;
// if distance is in KMs
double E = 40075;
// if distance is in Miles
//double E = 21638;
zoom = (byte) Math.round(Math.log(E / nearByRadius) / Math.log(2) + 1);
// to avoid exeptions
if (zoom > 21) {
zoom = 21;
}
if (zoom < 1) {
zoom = 1;
}
Log.v(TAG, "zoom level = " + zoom);
return zoom;
}
答案 0 :(得分:2)
我有类似的任务,我需要显示中心和N km半径的地图。 这是我的解决方案
private CameraUpdate getZoomForDistance(LatLng originalPosition,double distance){
LatLng rightBottom = SphericalUtil.computeOffset(originalPosition,distance,135);
LatLng leftTop = SphericalUtil.computeOffset(originalPosition,distance,-45);
LatLngBounds sBounds = new LatLngBounds(new LatLng(rightBottom.latitude,leftTop.longitude),new LatLng(leftTop.latitude,rightBottom.longitude));
return CameraUpdateFactory.newLatLngBounds(sBounds,0);
}
它使用此库android-maps-utils 希望这会对你有所帮助
答案 1 :(得分:0)
基于@alexandr响应
fun newLatLngOffset(center: LatLng, dist: Int): CameraUpdate {
return CameraUpdateFactory.newLatLngBounds(
LatLngBounds.Builder()
.include(SphericalUtil.computeOffset(center, dist.toDouble(), 0.0))
.include(SphericalUtil.computeOffset(center, dist.toDouble(), 90.0))
.include(SphericalUtil.computeOffset(center, dist.toDouble(), 180.0))
.include(SphericalUtil.computeOffset(center, dist.toDouble(), 270.0))
.build(), 0
)
}
主要区别是与他的解决方案圈子是外切的,在我的题词上是