Android Google Maps按纬度和经度缩放到特定区域 [英] android google maps zoom to a specific area by lat and long
本文介绍了Android Google Maps按纬度和经度缩放到特定区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何按级别缩放Google地图 但是我想要在Google地图上显示特定区域
I know how to zoom google maps by level but what I want is to display a specific area on google maps
我的意思是我只想显示纬度= 24.453&周围的区域.长= 35.547&半径= 200公里
I mean I want just to show the area around Lat = 24.453 & Long = 35.547 & Radius = 200km
如何实现?
推荐答案
Thanks to @Ken and How does this Google Maps zoom level calculation work? I got a solution.
private fun getZoomLevel(radius: Double): Float {
return if (radius > 0) {
// val scale = radius / 300 // This constant depends on screen size.
// Calculate scale depending on screen dpi.
val scale = radius * resources.displayMetrics.densityDpi / 100000
(16 - Math.log(scale) / Math.log(2.0)).toFloat()
} else 16f
}
我不知道它如何取决于屏幕尺寸(dpi,宽度,高度),所以这是另一个变体:
I don't know how it depends on screen size (dpi, width, height), so this is another variant:
private fun getZoomLevel(radius: Double): Float {
return if (radius > 0) {
val metrics = resources.displayMetrics
val size = if (metrics.widthPixels < metrics.heightPixels) metrics.widthPixels
else metrics.heightPixels
val scale = radius * size / 300000
(16 - Math.log(scale) / Math.log(2.0)).toFloat()
} else 16f
}
用法:
private fun zoomMapToRadius(latitude: Double, longitude: Double, radius: Double) {
val position = LatLng(latitude, longitude)
val center = CameraUpdateFactory.newLatLngZoom(position, getZoomLevel(radius))
googleMap?.animateCamera(center)
}
或
private fun zoomMapToRadius(/*latitude: Double, longitude: Double,*/ radius: Double) {
//val position = LatLng(latitude, longitude)
//val center = CameraUpdateFactory.newLatLng(position)
//googleMap?.moveCamera(center)
val zoom = CameraUpdateFactory.zoomTo(getZoomLevel(radius))
googleMap?.animateCamera(zoom)
}
更新
一个更准确的解决方案在这里: https://stackoverflow.com/a/56464293/2914140 .
A more accurate solution is here: https://stackoverflow.com/a/56464293/2914140.
这篇关于Android Google Maps按纬度和经度缩放到特定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文