Map v2 convert distance to pixels - android

I have been searching but I couldn't find it. I am using google map v2. Is there any way I can convert distance ( meters) to pixels on my screen? I need the pixels equivalent of the distance. I have the distance and I have the zoom level.
Any help is appreciated.

If I understand correctly, you have a MapFragment on your screen showing a map. You know the distance in meters between 2 points on the map that are shown on the screen and you want to calculate the distance between those two points in pixels. If you know the LatLng location of the two points, you can use the Projection class like this:
Point point1 = map.getProjection().toScreenLocation(latLng1);
Point point2 = map.getProjection().toScreenLocation(latLng2);
and then you just need to use the distance mathematic formula:

Related

Longest distance displayed on screen in kilometers

On Google Map Api V2 for Android,
I would like to calculate the longest distance in kilometers displayed on screen (while zoom factor is a parameter) in order to determinate the radius of the smallest circle containing all the map displayed on screen at present time:
That means the distance between the center of the screen and one of the edges.
Is there some Tool I can use to do that ? (Maybe to get the coordinates of one of the edges...)
You can probably use the Projection class (http://developer.android.com/reference/com/google/android/gms/maps/Projection.html)
For example:
Projection projection = map.getProjection(); // get map projection
VisibleRegion vr = projection.getVisibleRegion(); //
vr now contains the corners of your map, from there you can compute the distances...
http://developer.android.com/reference/com/google/android/gms/maps/model/VisibleRegion.html
It would probably be wise to do this inside an OnCameraChangedListener , to be sure you have valid a projection value

Pan map in a single direction

My V2 Map has been set in a random position, with a random bearing from north (so the compass symbol is likely to be pointing anywhere, but probably not north).
I want to shift/pan the map directly up the screen as the user looks at it, a short distance.
This may be very simple or require some complex calculations with lats, longs and trigonometry.
But I'm struggling to find a solution that works whatever the angle and zoom level is.
How can i do this ?
Well I solved this by finding a position on the screen and converting it to a LatLng, and then panning the map to this position. It worked perfectly.
Projection projection = mapView.getProjection();
Point p = new Point(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
LatLng pos = projection.fromScreenLocation(p);

Retrieve distance from visible part of Google map

I want to draw a static circle over a map with Google Maps. When the user pinches, the map will zoom in/out.
I need to know the map radius (related to the area contained in the circle) and change the seekbar at the bottom accordingly.
Does anybody know a solution how to retrieve the distance from the left to the right screen edge? I didn't find anything at the Google Maps API doc.
Something like this:
using VisibleRegion you can get the all corner cordinates and also the center.
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
and you can calcuate distance from two region by this
Location MiddleLeftCornerLocation;//(center's latitude,vr.latLngBounds.southwest.longitude)
Location center=new Location("center");
center.setLatitude( vr.latLngBounds.getCenter().latitude);
center.setLongitude( vr.latLngBounds.getCenter().longitude);
float dis = center.distanceTo(MiddleLeftCornerLocation);//calculate distane between middleLeftcorner and center
I got a while to write the code for "MiddleLeftCornerLocation" variable in the great answer given by #Md. Monsur Hossain Tonmoy, so just to complete it (I haven't enough reputation points to comment your answer, sorry):
VisibleRegion vr = map.getProjection().getVisibleRegion();
double bottom = vr.latLngBounds.southwest.latitude;
Location center = new Location("center");
center.setLatitude(vr.latLngBounds.getCenter().latitude);
center.setLongitude(vr.latLngBounds.getCenter().longitude);
Location middleLeftCornerLocation = new Location("center");
middleLeftCornerLocation.setLatitude(center.getLatitude());
middleLeftCornerLocation.setLongitude(left);
float dis = center.distanceTo(middleLeftCornerLocation);
First get the map center point with
googleMap.getCameraPosition().target;
then figure out the width of the map and get the height/2 and convert them to a Point using the x y values you just got.
then relate that point to the map
LatLng widthPoint = map.getProjection().fromScreenLocation(point);
now that you have the tagret point and the width point you can calculate the distance between the 2 points which will give you the radius.
Note
this assumes always in portrait mode, if you are in landscape mode the circle will be bigger than the visible area so in this case you will want to get the distance from the height

android How to convert pixel coordinates to LatLng on mapsv2

I've looked on the net but couldn't find an accurate answer. I have an ImageView to use as a marker centred on map, Its position not change when I pan the map. I want to get the latlng of position when I stop panning on the map. I guess I have to use projection but because of I am not so expert in android I got stuck. Can anybody help please.
Use map.getProjection().fromScreenLocation(Point) and map.getProjection().toScreenLocation(LatLng) to convert between screen position and geo location.
Use the Projection via map.getProjection().
This will allow you to convert between screen coordinates and LatLngs. The pixels returned are relative to the View containing the map.
For Maps API v2
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
Also you can use,
LatLngBounds curScreen = mMap.getProjection()
.getVisibleRegion().latLngBounds;
Hope this will help.

How to convert a LatLng and a radius to a LatLngBounds in Android Google Maps API v2?

I believe this is a limitation of the recent Google Maps API v2. They have recently added the ability to draw a Circle on the ground - but if you want to position the camera such that it shows the entire Circle, there exists no way to do so.
One can call CameraUpdateFactory#newLatLngBounds(bounds, padding) where "bounds" is a LatLngBounds and "padding" is a distance in pixels. The issue is that there is no way to create a LatLng and a radius into a LatLngBounds.
The constructor for LatLngBounds only takes 2 LatLng instances and generates a rectangle where these are the NW and SE corners.
Just like Risadinha mentioned, you can easily achieve that with android-maps-utils. Just add:
compile 'com.google.maps.android:android-maps-utils:0.4.4'
to your gradle dependencies, use the following code:
public LatLngBounds toBounds(LatLng center, double radiusInMeters) {
double distanceFromCenterToCorner = radiusInMeters * Math.sqrt(2.0);
LatLng southwestCorner =
SphericalUtil.computeOffset(center, distanceFromCenterToCorner, 225.0);
LatLng northeastCorner =
SphericalUtil.computeOffset(center, distanceFromCenterToCorner, 45.0);
return new LatLngBounds(southwestCorner, northeastCorner);
}
EDIT:
Our goal is to calculate two points (LatLngs):
southwestCorner
and
northeastCorner
From the javadoc of the SphericalUtil you can read that 225 and 45 are heading values, and the distanceFromCenterToCorner is the distance. Further explanation of the values in the picture below:
With the javascript library you can draw a circle with a center and radius and then get its bounds.
centerSfo = new google.maps.LatLng(37.7749295, -122.41941550000001);
circle = new google.maps.Circle({radius: 5000, center: centerSfo});
bounds = circle.getBounds();
You could do the same using the android api.
This is totally doable.
The LatLng is the center of your circle correct? What you want to do is inscribe your circle inside of the LatLngBounds (Circle inside a Square problem), so the entire thing will show up on the map.
If you draw this on paper you can see that you have everything you need to calculate your LatLngBounds.
Remember how to find the lengths of the sides of a right triangle?
a² + b² = c²
If you draw a line from the center of your circle to the NW (upper left) corner, and another straight to the Western wall (straight line from center, to the left) of the square you have a triangle. Now you can use the equation above to solve for c since you know the the length of the other sides of the triangle (the circle's radius).
So now your equation becomes
r² + r² = c²
which reduces to
2r² = c²
which further reduces to
c = squareRoot(2) * r
Now you have the distance. This is of course an oversimplification, because the Earth is not flat. If the distances aren't huge, you could use the same equation above, but modified to project a spherical earth onto a plane:
http://en.wikipedia.org/wiki/Geographical_distance#Flat-surface_formulae
Notice this also uses the Pythagorean theorem, same as we did above.
Next you will need to calculate your endpoints (NW, and SE corners) from your center point given a bearing, and the distance you found above.
This post may help: Calculate endpoint given distance, bearing, starting point
Don't forget to convert your degrees to radians when using the equation from the post linked above! ( Multiply degrees by pi/180 )
There is a utility library by Google for that:
http://googlemaps.github.io/android-maps-utils/
Recommended by Google for this task and example code:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=5704
While Bartek Lipinski's answer is correct if your LatLngBounds define a square, most LatLngBounds define rectangles, and as such, the bearings of the North-East and the South-West points from the center will not always be 45 and 255 degrees.
Therefore if you are looking to get the LatLngBounds of a radius from a center for any quadrilateral, use the coordinates of your initial bounds.northeast and bounds.southwest like this (using SphericalUtils):
LatLng northEast = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), SphericalUtil.computeHeading(center, bounds.northeast));
LatLng southWest = SphericalUtil.computeOffset(center, radius * Math.sqrt(2.0), (180 + (180 + SphericalUtil.computeHeading(center, bounds.southwest))));
(180 + (180 + x)) calculates the bearing of the South-West point from the center clockwise.

Categories

Resources