Is there any possible way of finding radius of the visible map from the middle point?
I want to get the near places against the center point of the map from an API, and that API require lat,lng and radius. I am able to get lat and lng from center point but couldnt find a way to get radius .
thanks
For the Google Maps Android API, you can get the bounds by...
From the map reference, get the Projection by getProjection(). And,
a projection is used to translate between on screen location and geographic coordinates..
So from the projection, we can use the getVisibleRegion(), and to get the VisibleRegion of the map, which contains a LatLngBounds, which is a class that contains 2 LatLng variables, one for the Northeast corner of the bound and one for the Southwest corner.
So the code should look something like this:
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition position) {
LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
LatLng northeast = bounds.northeast;
LatLng southwest = bounds.southwest;
Context context = getApplicationContext();
CharSequence text = "ne:"+northeast+" sw:"+southwest;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
=-=-=-=-=-=
edit:
May be I was too naive, given only the NE and SW can solve this problem, but only under the special case where user did not rotate the map or tilt up for the 3D map.
So instead, you can just grab the VisibleRegion, which provided 4 variable, farRight, farLeft, nearRight, nearLeft, each represent 4 conners of the area.
Then we can calculate the width and height of the area for that 4 points and pick the smaller one (well, sometime width can be greater than height I guess.)
And for the calculation, we can just use the Location.distanceBetween(x1,y1,x2,y2,result) function...
which makes the code look like the following:
VisibleRegion visibleRegion = googleMap.getProjection().getVisibleRegion();
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
float[] distanceWidth = new float[2];
Location.distanceBetween(
(farRight.latitude+nearRight.latitude)/2,
(farRight.longitude+nearRight.longitude)/2,
(farLeft.latitude+nearLeft.latitude)/2,
(farLeft.longitude+nearLeft.longitude)/2,
distanceWidth
);
float[] distanceHeight = new float[2];
Location.distanceBetween(
(farRight.latitude+nearRight.latitude)/2,
(farRight.longitude+nearRight.longitude)/2,
(farLeft.latitude+nearLeft.latitude)/2,
(farLeft.longitude+nearLeft.longitude)/2,
distanceHeight
);
float distance;
if (distanceWidth[0]>distanceHeight[0]){
distance = distanceWidth[0];
} else {
distance = distanceHeight[0];
}
thank you so much for your answer #kaho, it helped me alot (even you calculated the distanceWidth and distanceHeight in the same way).
Clarification:
farLeft LatLng object that defines the top left corner of the camera.
farRight LatLng object that defines the top right corner of the camera.
nearLeft LatLng object that defines the bottom left corner of the camera.
nearRight LatLng object that defines the bottom right corner of the camera.
EDITED: I don't know why we made a simple calculation become a bit complicated, the visible radius is just A HALF OF VISIBLE DIAGONAL LINE, that's all!
private double getMapVisibleRadius() {
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
float[] diagonalDistance = new float[1];
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
Location.distanceBetween(
farLeft.latitude,
farLeft.longitude,
nearRight.latitude,
nearRight.longitude,
diagonalDistance
);
return diagonalDistance[0] / 2;
}
I also logged my results to compare with #jossef-harush 's results and it's approximately:
Full area, even corners!
I don't see other answers cover the entire map area;
see image below, to test it I drew a circle overlay to see the bounds of the calculated radius, it does not cover entire map area.
my modification is quite simple, I've used Pythagorean theorem to find the suitable radius to contain the map "rectangle".
private double getMapVisibleRadius() {
VisibleRegion visibleRegion = googleMap.getProjection().getVisibleRegion();
float[] distanceWidth = new float[1];
float[] distanceHeight = new float[1];
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
Location.distanceBetween(
(farLeft.latitude + nearLeft.latitude) / 2,
farLeft.longitude,
(farRight.latitude + nearRight.latitude) / 2,
farRight.longitude,
distanceWidth
);
Location.distanceBetween(
farRight.latitude,
(farRight.longitude + farLeft.longitude) / 2,
nearRight.latitude,
(nearRight.longitude + nearLeft.longitude) / 2,
distanceHeight
);
double radiusInMeters = Math.sqrt(Math.pow(distanceWidth[0], 2) + Math.pow(distanceHeight[0], 2)) / 2;
return radiusInMeters;
}
For the Kotlin users, call this function from setOnCameraIdleListener
private fun getMapVisibleRadius(): Double {
val visibleRegion: VisibleRegion = mMap.projection.visibleRegion
val distanceWidth = FloatArray(1)
val distanceHeight = FloatArray(1)
val farRight: LatLng = visibleRegion.farRight
val farLeft: LatLng = visibleRegion.farLeft
val nearRight: LatLng = visibleRegion.nearRight
val nearLeft: LatLng = visibleRegion.nearLeft
Location.distanceBetween((farLeft.latitude + nearLeft.latitude) / 2, farLeft.longitude, (farRight.latitude + nearRight.latitude) / 2, farRight.longitude, distanceWidth)
Location.distanceBetween(farRight.latitude,
(farRight.longitude + farLeft.longitude) / 2, nearRight.latitude, (nearRight.longitude + nearLeft.longitude) / 2, distanceHeight)
val radiusInMeters = Math.sqrt((Math.pow(distanceWidth.get(0).toString().toDouble(), 2.0))
+ Math.pow(distanceHeight.get(0).toString().toDouble(), 2.0)) / 2
return radiusInMeters
}
edit: The following answer is for Google Maps JavaScript API v3
=-=-=-=-=-=-=
I think the answer would be: Yes, you can.
According to the documentation, you can calculate distance between 2 points by: computeDistanceBetween(LatLngFrom, LatLngTo)
Also you can get the boundary of the map by using getBounds() method, which is in the google.maps.Map class.
Related
I am trying to create heart shape polygon on Google Map using current location. I am able to identify some LatLngs and tried to create heart shape but as expected curves are not showing.
Can you please help me out to create exact heart shape polygon using current location.
Here is code which I am using to create heart shape.
private static final int FRONT = 0;
private static final int RIGHT = 90;
private static final int LEFT = 270;
private void drawHeartPolygon(LatLng currentLatLng) {
LatLng destLatLang = GetDestinationPoint(currentLatLng, FRONT, 0.050F);
frontAngleCalculation(currentLatLng, destLatLang, 0.050F);
}
private void frontAngleCalculation(LatLng latLng, LatLng destLatLang, float distance) {
PolygonOptions rectOptions = new PolygonOptions();
LatLng centerLocation = GetDestinationPoint(latLng, FRONT, (distance + (distance/4)/2)/2);
LatLng rightLocation = GetDestinationPoint(centerLocation, RIGHT, distance/2);
LatLng leftLocation = GetDestinationPoint(centerLocation, LEFT, distance/2);
LatLng centerLeftLocation = GetDestinationPoint(destLatLang, LEFT, distance/4);
LatLng centerLeftTopLocation = GetDestinationPoint(centerLeftLocation, FRONT, (distance/4)/2);
LatLng centerRightLocation = GetDestinationPoint(destLatLang, RIGHT, distance/4);
LatLng centerRightTopLocation = GetDestinationPoint(centerRightLocation, FRONT, (distance/4)/2);
rectOptions.add(new LatLng(latLng.latitude, latLng.longitude),
leftLocation,
centerLeftTopLocation,
new LatLng(destLatLang.latitude, destLatLang.longitude),
centerRightTopLocation,
rightLocation);
Log.d(TAG, "Current Location : "+latLng);
rectOptions.strokeColor(Color.RED);
// Get back the mutable Polygon
Polygon polygon = mMap.addPolygon(rectOptions);
List<PatternItem> pattern = Arrays.<PatternItem>asList(
new Dot(), new Gap(20), new Dash(30), new Gap(20));
polygon.setStrokePattern(pattern);
polygon.setStrokeWidth(POLYGON_STROKE_WIDTH_PX);
polygon.setStrokeColor(strokeColor);
}
public static LatLng GetDestinationPoint(LatLng startLoc, float bearing, float depth) {
LatLng newLocation = null;
double radius = 6371.0; // earth's mean radius in km
double lat1 = Math.toRadians(startLoc.latitude);
double lng1 = Math.toRadians(startLoc.longitude);
double brng = Math.toRadians(bearing);
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(depth / radius) + Math.cos(lat1) * Math.sin(depth / radius) * Math.cos(brng));
double lng2 = lng1 + Math.atan2(Math.sin(brng) * Math.sin(depth / radius) * Math.cos(lat1), Math.cos(depth / radius) - Math.sin(lat1) * Math.sin(lat2));
lng2 = (lng2 + Math.PI) % (2 * Math.PI) - Math.PI;
// normalize to -180...+180
if (lat2 == 0 || lng2 == 0) {
newLocation = new LatLng(0.0, 0.0);
} else {
newLocation = new LatLng(Math.toDegrees(lat2), Math.toDegrees(lng2));
}
return newLocation;
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady");
mMap = googleMap;
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.setMaxZoomPreference(22.0f);
mMap.setMinZoomPreference(17.0f);
drawHeartPolygon(new LatLng(latitude, longitude));
}
Here is screenshot which shows heart shape which I achieved but not as per expectation.
Please give me reference or hint which will draw heart shape polygon with curves.
I don't know whether you'll like this answer or not but this will work as your requirement and is easy as hell but different.
Just place a marker:
LatLng latLng1 = new LatLng(13.014849, 80.224343);
mMap.addMarker(new MarkerOptions().position(latLng1).title("Name").snippet("snippet").flat(true).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1)));
CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng1).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Here, marker1.png is an image created through photoshop which will provide the same result.
Result:
Marker1.png:
As you can see marker1 here is a whole image containing the marker + the heart but you can also create two markers on same LatLng: 1st is the red marker and 2nd is the Heart. Using this way, infowindow will open only on clicking the red marker instead of the provided result as you can disable the heart's infowindow or you can use heart's infowindow for some other info.
As I said earlier this solution is different and isn't similar to custom shaped polygon but is super easy to achieve.
Alright so after reading Antonio's comment, I am with this in my code. Now regardless of what I submit as my percentage it still thinks my object is outside the bounding box.
My Position is the marker passed in.
LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
LatLngBounds newBounds = reduceBy(bounds, 0.05d);
if(newBounds.contains(myPosition.getPosition())) {
//If the item is within the the bounds of the screen
} else{
//If the marker is off screen
zoomLevel -= 1;}
}
return zoomLevel;
}
public LatLngBounds reduceBy(LatLngBounds bounds, double percentage) {
double distance = SphericalUtil.computeDistanceBetween(bounds.northeast, bounds.southwest);
double reduced = distance * percentage;
double headingNESW = SphericalUtil.computeHeading(bounds.northeast, bounds.southwest);
LatLng newNE = SphericalUtil.computeOffset(bounds.northeast, reduced/2d, headingNESW);
double headingSWNE = SphericalUtil.computeHeading(bounds.southwest, bounds.northeast);
LatLng newSW = SphericalUtil.computeOffset(bounds.southwest, reduced/2d, headingSWNE);
return LatLngBounds.builder().include(newNE).include(newSW).build();
}
}
I have all the zoom levels set but sometimes I run into spots such as this where it is still in bounds except the marker is off screen. I want to have a slightly smaller bounding box to detect this and then zoom out one level on only these situations.
You can use SphericalUtil class from the Google Maps API Utility Library to make the calculations:
public LatLngBounds reduceBy(LatLngBounds bounds, double percentage) {
double distance = SphericalUtil.computeDistanceBetween(bounds.northeast, bounds.southwest);
double reduced = distance * percentage;
double headingNESW = SphericalUtil.computeHeading(bounds.northeast, bounds.southwest);
LatLng newNE = SphericalUtil.computeOffset(bounds.northeast, reduced/2d, headingNESW);
double headingSWNE = SphericalUtil.computeHeading(bounds.southwest, bounds.northeast);
LatLng newSW = SphericalUtil.computeOffset(bounds.southwest, reduced/2d, headingSWNE);
return LatLngBounds.builder().include(newNE).include(newSW).build();
}
To reduce your bounds by a 5% (diagonal) you can do:
LatLngBounds newBounds = reduceBy(bounds, 0.05d);
Depending on your requirements for precision, you might want to just use simple interpolation like here:
public LatLngBounds reduceBounds(LatLngBounds bounds, double percentage) {
double north = bounds.northeast.latitude;
double south = bounds.southwest.latitude;
double east = bounds.northeast.longitude;
double west = bounds.southwest.longitude;
double lowerFactor = percentage / 2 / 100;
double upperFactor = (100 - percentage / 2) / 100;
return new LatLngBounds(new LatLng(south + (north - south) * lowerFactor, west + (east - west) * lowerFactor),
new LatLng(south + (north - south) * upperFactor, west + (east - west) * upperFactor));
}
This is very simple Math using +-*/ and doesn't cost a lot of performance.
To reduce your bounds dimensions by 10% you do:
LatLngBounds newBounds = reduceBounds(bounds, 10);
Add error checking and border case handling as needed
I'm trying to add an image to a Google Map using the Google Maps Android API v2:
mMap = googleMap;
LatLng sw = new LatLng(47.01377857060625, 8.305519705172628);
LatLng ne = new LatLng(47.01395211967171, 8.306270482717082);
LatLng nw = new LatLng(47.014014755501165, 8.305559697328135);
LatLng se = new LatLng(47.01370751919609, 8.306236284552142);
LatLngBounds latLngBounds = new LatLngBounds(sw, ne).including(nw).including(se);
GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAsset("t2c.png");
groundOverlayOptions.image(bitmapDescriptor);
groundOverlayOptions.positionFromBounds(latLngBounds);
mMap.addGroundOverlay(groundOverlayOptions);
Unfortunately, the LatLng values are not 100% accurate, so the background image doesn't rotate but is skewed according to Google:
If the bounds do not match the original aspect ratio, the image will be skewed.
Using only LatLng sw and ne:
https://developers.google.com/maps/documentation/android-api/groundoverlay#use_latlngbounds_to_position_an_image
I don't know how I should be able to figure out the exact LatLng of south-west and north-east, so I'm interested in a way to define a polygon and squeeze the image somehow into it with the four LatLng as anchors. Using four LatLng currently looks like this. Using LatLng sw, ne, nw and se:
you will never achieve what you want using LatLngBounds because you are trying to rotate your GroundOverlay.
What you need to do is to use bearing (rotation) and width/height of the ground overlay.
Let's say your building in -20 degrees rotated and has width-height of 40-20 (only you know these values.
1- Get the LatLng of the center of the building, which is finding the center of the four coordinates in your code above.
That value will be your "centerLocation"
GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromAsset("t2c.png");
groundOverlayOptions.image(bitmapDescriptor);
groundOverlayOptions.position(centerLocation,widthInMeters,heightInMeters);
groundOverlayOptions.bearing(rotated);//the value is clockwise and rotation is about anchor point (which should be by default 0.5,0.5 of your image
mMap.addGroundOverlay(groundOverlayOptions);
This should work, obviously you have to calculate values or go step by step trying different values.
Hope it helps
The first answer is what will works for u. I will just write my solution in detail so it could be easy to just copy paste code and get it done...
First Step if u have four bounds, get center of them for which u can use the below the function
private LatLng computeCentroid(List<LatLng> points) {
double latitude = 0;
double longitude = 0;
int n = points.size();
for (LatLng point : points) {
latitude += point.latitude;
longitude += point.longitude;
}
return new LatLng(latitude/n, longitude/n);
}
Second get the width and height in meters. Here are method to get distance between two points in meters.
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return dist * 1609.34;
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
Last just use these values and done
double height=distance(selectedFloorPlan.getGeoData().get(0).getLat(), selectedFloorPlan.getGeoData().get(0).getLng(),selectedFloorPlan.getGeoData().get(1).getLat(), selectedFloorPlan.getGeoData().get(1).getLng());
double width=distance(selectedFloorPlan.getGeoData().get(0).getLat(), selectedFloorPlan.getGeoData().get(0).getLng(),selectedFloorPlan.getGeoData().get(3).getLat(), selectedFloorPlan.getGeoData().get(3).getLng());
GroundOverlayOptions newarkMap = new GroundOverlayOptions()
.image(images.get(0))
.position(centerPoint,(float)width,(float)height)
.bearing(-67.18571490414709f);
imageOverlay = map.addGroundOverlay(newarkMap);
In my Android application I want to request data for the location where the user touches the map. GoogleMap.OnMapClickListener provides the touched position as latitude and longitude coordinates.
public abstract void onMapClick(LatLng point)
In order to pass an area instead of a point I need to calculate the coordinates for a bounding box centered at the touch position. The extend of the bounding box should not depend on the zoom level of the map.
I do not want to request the bounding box of the visible screen - just a small bounding box area around the touch position.
Is there any framework method I could use? Otherwise, how do I find a suitable distance value for the extend of the bounding box?
Isn't LatLngBounds what you need?
You can override onMarkerClick like below
#Override
public boolean onMarkerClick(Marker marker) {
if (markerClicked) {
if (polygon != null) {
polygon.remove();
polygon = null;
}
polygonOptions.add(marker.getPosition());
polygonOptions.strokeColor(ContextCompat.getColor(getContext(), R.color.colorRedTransparent));
polygonOptions.fillColor(ContextCompat.getColor(getContext(), R.color.colorBlueTransparent));
polygonOptions.strokeWidth(5.0f);
polygon = mMap.addPolygon(polygonOptions);
polygon.setClickable(true);
} else {
if (polygon != null) {
polygon.remove();
polygon = null;
}
polygonOptions = new PolygonOptions().add(marker.getPosition());
markerClicked = true;
}
return true;
}
Pass your polygon to generate bounding box
public static Rectangle getBoundingBox(Polygon polygon) {
double boundsMinX = Double.MAX_VALUE; // bottom south latitude of the bounding box.
double boundsMaxX = Double.MIN_VALUE; // top north latitude of bounding box.
double boundsMinY = Double.MAX_VALUE; // left longitude of bounding box (western bound).
double boundsMaxY = Double.MIN_VALUE; // right longitude of bounding box (eastern bound).
for (int i = 0; i < polygon.getPoints().size(); i++) {
double x = polygon.getPoints().get(i).latitude;
boundsMinX = Math.min(boundsMinX, x);
boundsMaxX = Math.max(boundsMaxX, x);
double y = polygon.getPoints().get(i).longitude;
boundsMinY = Math.min(boundsMinY, y);
boundsMaxY = Math.max(boundsMaxY, y);
}
//Rectangle(double left, double bottom, double right, double top)
return new Rectangle(boundsMinY, boundsMinX, boundsMaxY, boundsMaxX);
}
I'm not sure I fully understand your question, but usually screen-to-map coordinates are done using a few methods, probably something like that:
Projection projection = googleMaps.getProjection();
Point p = projection.toScreenLocation(point);
LatLng topLeft = projection.fromScreenLocation(new Point(p.x - halfWidth, p.y - halfHeight));
LatLng bottomRight = projection.fromScreenLocation(new Point(p.x + halfWidth, p.y + halfHeight));
Edit:
on the above
point is the LatLng you received on your onMapClick.
p is the on screen position of said click event
p.x - halfWidth, p.y - halfHeight, p.x + halfWidth, p.y + halfHeight is a bounding box around the click location with 2 * halfWidth of width and 2 * halfHeight of height.
topLeft and bottomRight is a bounding box around the click event in Latitude-Longitude coordinates.
Is there a way to get the coordinates of the current area, which is shown at the device?
Background is, we want to show "nearby" places, which are stored in our own database. So let's say, the user looks at following clip of a map:
How do we get the longitude/latitude of the screen (or the point in the middle of the screen and a radius which covers everything?). Please keep in mind, center of the map is not usually the current position, since the user can move the center of the card!
Use map.getProjection().getVisibleRegion(). From VisibleRegion you can get LatLngBounds, which is easy to work with. You may also try directly with the region, which might be trapezoid.
I found the solution for Google Map API v2 from few of responses:
stackoverflow#1 and
stackoverflow#2
So, need implements Activity from GoogleMap.OnCameraChangeListener interface
private static final int REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR = -1;
private static final double EARTH_RADIOUS = 3958.75; // Earth radius;
private static final int METER_CONVERSION = 1609;
private GoogleMap mGoogleMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (status != ConnectionResult.SUCCESS)
{
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, activity,
REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR);
dialog.show();
mGoogleMap = null;
}
else
{
mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
R.id.fragment_shops_layout_maps_fragment)).getMap();
mGoogleMap.setOnCameraChangeListener(this);
}
}
The listener, that working when map scaled. Determin as LatLng the positions of bottom left, bottom right, top left and top right sides of map, that showing on screen. By greatest side of screen and two points we can get radius from center of map.
#Override
public void onCameraChange(CameraPosition cameraPosition)
{
// Listener of zooming;
float zoomLevel = cameraPosition.zoom;
VisibleRegion visibleRegion = mGoogleMap.getProjection().getVisibleRegion();
LatLng nearLeft = visibleRegion.nearLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng farRight = visibleRegion.farRight;
double dist_w = distanceFrom(nearLeft.latitude, nearLeft.longitude, nearRight.latitude, nearRight.longitude);
double dist_h = distanceFrom(farLeft.latitude, farLeft.longitude, farRight.latitude, farRight.longitude);
Log.d("DISTANCE: ", "DISTANCE WIDTH: " + dist_w + " DISTANCE HEIGHT: " + dist_h);
}
Return distance between 2 points, stored as 2 pair location at meters;
public double distanceFrom(double lat1, double lng1, double lat2, double lng2)
{
// Return distance between 2 points, stored as 2 pair location;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = EARTH_RADIOUS * c;
return new Double(dist * METER_CONVERSION).floatValue();
}
If you want get radius of area, that showed on screen just need devided by 2.
I hope will useful !
This calculates the radio in km based on the map width:
public double calculateVisibleRadius() {
float[] distanceWidth = new float[1];
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
LatLng farRight = visibleRegion.farRight;
LatLng farLeft = visibleRegion.farLeft;
LatLng nearRight = visibleRegion.nearRight;
LatLng nearLeft = visibleRegion.nearLeft;
//calculate the distance between left <-> right of map on screen
Location.distanceBetween( (farLeft.latitude + nearLeft.latitude) / 2, farLeft.longitude, (farRight.latitude + nearRight.latitude) / 2, farRight.longitude, distanceWidth );
// visible radius is / 2 and /1000 in Km:
return distanceWidth[0] / 2 / 1000 ;
}