Longest distance displayed on screen in kilometers - android

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

Related

Map v2 convert distance to pixels

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:

Android Maps Projection fromScreenLocation returns wrong geo position

The Google Map provides over its projection a method, which converts Point to Geo Position.
If I pass e.x. Point p = new Point(-1000,1000), I get a position like -89.3425,140.0345. The point is outside the visible region but I could not found any information if that method has a limitation to the current visible region.
Has someone a source code or more information about that ?
Create a Rect with the size of your MapView and use Rect.contains(x,y) to check if the given point is inside the MapView
Remember:
The screen location is specified in screen pixels (not display pixels)
relative to the top left of the map (not the top left of the whole
screen).

Attain maximum zoom on google map showing all markers

i have 5 markers to display on the map, out of which 4 are very near to each other and the fifth one is a little bit distant to these 4. now when i display the map i want all these 5 markers to be shown on map and the with the highest possible zoom. i dont care whether they are on the border of the screen or in the center of the screen.i mean the markers can be scattered on the screen but all i want is that all markers should visible to the user and with the highest possible zoom.
i have tried this Android map v2 zoom to show all the markers . but the result is that it is showing all markers at the center of the map with very little zoom. actually i have calculated screen dimensions using this code.
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float height=metrics.heightPixels/metrics.xdpi;
float width=metrics.widthPixels/metrics.ydpi;
but i dont know why there is very little zoom. But wen i saw android documentation, i think the function is doing justice by doing whatever it said it will do.
public static CameraUpdate newLatLngBounds (LatLngBounds bounds, int width, int height, int padding)
Returns a CameraUpdate that transforms the camera such that the specified
latitude/longitude bounds are centered on screen within a bounding box of specified
dimensions at the greatest possible zoom level. You can specify additional padding,
to further restrict the size of the bounding box. The returned CameraUpdate has a
bearing of 0 and a tilt of 0.
Unlike newLatLngBounds(LatLngBounds, int), you can use the CameraUpdate returned by
this method to change the camera prior to the map's the layout phase, because the
arguments specify the desired size of the bounding box.
as it says it keeps all the markers at the center of the map. i do not want that. i want all the markers visible to the user with the maximum possible zoom and markers scattered. can anybody please help me?
Your calculation of float width and height is incorrect.
What your width holds now is inches (value of approx. 2 on phones). You need not to divide pixels width.

Google MapView zoomout to span a circle

I have a MapView centered at point P. The user can't change the MapView center, but he can choose a radius of a circle to be display around point P, and change it dynamically with the map being redrawn at each change to show the new circle.
The thing is, i want the map to zoom in or out as necessary, in order to display the entire circle at the viewable area. I've tried this:
Projection proj = m_Map.getProjection();
Point mapCenterPixles = new Point();
proj.toMapPixels(center, mapCenterPixles);
float radiusPixels = proj.metersToEquatorPixels(newRadius);
IGeoPoint topLeft = proj.fromPixels(mapCenterPixles.x - radiusPixels,
mapCenterPixles.y - radiusPixels);
IGeoPoint bottomRight = proj.fromPixels(mapCenterPixles.x
+ radiusPixels, mapCenterPixles.y + radiusPixels);
m_Map.getController().zoomToSpan(
topLeft.getLatitudeE6() - bottomRight.getLatitudeE6(),
topLeft.getLongitudeE6() - bottomRight.getLongitudeE6());
But it seems i'm missing something, as the values passed to zoomToSpan() cause no chnage, I'm kind of lost here, can someone please shed some light on how to zoom the map to span a bounding box of the circle given its radius in meters, and its center points?
Google Maps zoom levels are defined in powers of two, so MapController#zoomToSpan() also zooms by powers of two.
Ergo, if the span you compute above is already displayable within the current zoom level, it's likely nothing would actually change visually in the map until you need to go to the next larger or smaller zoom level.
This behavior is somewhat vaguely described in the documentation for MapController#zoomToSpan

Mapping on a image programmatically

Client given me a particular area map image. Here I need to get the longitude and latitude of the location at the Touch point on the image.
Is there any way I can work on this issue. To get the location of touch point on the image programmatically.
You can really only do this with any accuracy on a MapView, where you can use the methods
GeoPoint fromPixels(int x,
int y)
Create a new GeoPoint from pixel coordinates relative to the top-left
of the MapView that provided this PixelConverter.
and
toPixels
android.graphics.Point toPixels(GeoPoint in,
android.graphics.Point out)
Converts the given GeoPoint to onscreen pixel coordinates, relative to
the top-left of the MapView that provided this Projection.
to transform lat/on to screen coordinates.
If it's a plain old .png the, as long as the view covers a small area (a few square miles only), you could interpolate the lat/lon at the corners to get an approximate value for the touch point. If it was a whole country, then you would have to apply a coordinate transformation to map from a flat Mercator projection to a curved surface.

Categories

Resources