1

I know how to zoom google maps by level but what I want is to display a specific area on google maps

I mean I want just to show the area around Lat = 24.453 & Long = 35.547 & Radius = 200km

How to achieve that?

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
Ken
  • 295
  • 1
  • 6
  • 18

2 Answers2

1
 CameraUpdate center=
    CameraUpdateFactory.newLatLng(new LatLng( 24.453,
                                             35.547));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

map.moveCamera(center);
map.animateCamera(zoom);

Use this, hope this will help you.

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");

Use this api service to use radius

Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
  • This will zoom by Level 15, not by radius. is there anyway I can specify the radius needed? – Ken Apr 27 '15 at 12:41
1

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
}

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
}

Usage:

private fun zoomMapToRadius(latitude: Double, longitude: Double, radius: Double) {
    val position = LatLng(latitude, longitude)
    val center = CameraUpdateFactory.newLatLngZoom(position, getZoomLevel(radius))
    googleMap?.animateCamera(center)
}

or

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)
}

UPDATE

A more accurate solution is here: https://stackoverflow.com/a/56464293/2914140.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • what is this 30000 factor? – Ankit Khare Dec 05 '19 at 11:10
  • @AnkitKhare, that was a constant found during experiments with different devices. Anyway, this is not a good solution, see a right answer here: https://stackoverflow.com/a/56464293/2914140. – CoolMind Dec 05 '19 at 12:28