【发布时间】:2015-04-27 12:31:35
【问题描述】:
我知道如何按级别缩放谷歌地图 但我想要的是在谷歌地图上显示特定区域
我的意思是我只想显示 Lat = 24.453 & Long = 35.547 & Radius = 200km 周围的区域
如何实现?
【问题讨论】:
-
你为什么不回答你的问题?
标签: android google-maps
我知道如何按级别缩放谷歌地图 但我想要的是在谷歌地图上显示特定区域
我的意思是我只想显示 Lat = 24.453 & Long = 35.547 & Radius = 200km 周围的区域
如何实现?
【问题讨论】:
标签: android google-maps
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng( 24.453,
35.547));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
使用这个,希望对你有帮助。
StringBuilder sb = new StringBuilder(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
sb.append("location=" + mLatitude + "," + mLongitude);
sb.append("&radius=" + radius);
sb.append("&types=" + type);
sb.append("&sensor=true");
sb.append("&key=YOUR_API_KEY");
使用这个api服务来使用radius
【讨论】:
感谢@Ken 和How does this Google Maps zoom level calculation work? 我找到了解决方案。
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、宽度、高度),所以这是另一个变体:
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。
【讨论】: