Android Google Maps Zoom To A Specific Area By Lat And Long
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 &am
Solution 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
Solution 2:
Thanks to @Ken and How does this Google Maps zoom level calculation work? I got a solution.
privatefungetZoomLevel(radius: Double): Float {
returnif (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()
} else16f
}
I don't know how it depends on screen size (dpi, width, height), so this is another variant:
privatefungetZoomLevel(radius: Double): Float {
returnif (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()
} else16f
}
Usage:
privatefunzoomMapToRadius(latitude: Double, longitude: Double, radius: Double) {
val position = LatLng(latitude, longitude)
val center = CameraUpdateFactory.newLatLngZoom(position, getZoomLevel(radius))
googleMap?.animateCamera(center)
}
or
privatefunzoomMapToRadius(/*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.
Post a Comment for "Android Google Maps Zoom To A Specific Area By Lat And Long"