android谷歌地图通过lat和long缩放到特定区域

时间:2015-04-27 12:31:36

标签: android google-maps

我知道如何按级别缩放谷歌地图 但我想要的是在谷歌地图上显示一个特定的区域

我的意思是我只想显示Lat = 24.453&的区域。长= 35.547&半径= 200km

如何实现?

2 个答案:

答案 0 :(得分:1)

 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

答案 1 :(得分:0)

感谢@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)
}