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?
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?
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
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.