Setting view on mapview based on gps coordinates - android

I'm trying to set the bounds on the mapview that I'm working with, so I'm always centered a certain way, and I can also see a full set of information based on the view. Basically I'll get a GPS coordinate for the center point, and then I'll get another value, say .9, which is the difference in the x/y of the current GPS spot that needs to be present in the current view. Example
Center-point: 37.777125, -12.2419644 (San Francisco)
Difference: .9
So the view would need to be
Top-Left: 36.877125, -13.1419644
Bottom-Right: 38.677125, -11.3419644
Is there a way to set the map view's bounds in this way?

It is. There is an interface in mapView's controller - zoomToSpan(long, long).
What you need is to do the following:
long latSpan = Math.abs(startLat - endLat);
long lonSpan = Math.abs(startLon - endLon);
mapView.getController().zoomToSpan(latSpan, lonSpan);
Where startLat, endLat, startLon, endLon - your calculated boundaries

Related

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

What is the unit of the coordinate system of Canvas? - android

I find that the unit of the coordinate system of Canvas is different from that of screen.
For example in my case as below:
For one particular point, its on-screen coordinate obtained from ImageView.getX() and ImageView.getY() is (336, 578).
Then by trial and error, I draw a dot on the Canvas so that this dot falls EXACTLY the same position as the ImageView. I called canvas.drawCircle(330, 440, radius, paint); to achieve this.
Here comes the question:
Why would the 2 coordinates, (336, 578) and (330, 440), different?
Is it because the screen and the canvas use different units?
Is it an issue regarding pixel, dp and all that?
You could try converting to and from window coordinates using View.getLocationInWindow this way you'll always be sure you are using the same coordinates system.
Coordinates of ImageView.getX an Y are relative to the parent. Canvas coordinates are relative to the view. This could cause the differences
Edit:
I assume in one place you get the coordinates of the view (using getX and getY). Convert these to window coordinates. Call these viewXY[].
When you need to use them, to draw in your canvas, also convert the top and left coordinates to window coordinates. We call these canvasXY[].
Now, the X position to draw at would be viewXY[0] - canvasXY[0], and the Y position would be viewXY[1] - canvasXY[1].
A view's position co-ordinates are always relative to it's parent. It's like saying, where with respect to it's parent is this view placed.
There in another thing, screen Co-ordinate. This says where with respect to the window top/left (think actual physical screen) is this object placed. This can be obtained through View.getLocationOnScreen.
If you want to compare two locations, they will only be equivalent if the parent view of both these locations is same or you're using absolute screen co-ordinates. Otherwise, you'll only get results that "seem" different.
If you want to unify the two either take them to a common parent or use absolute co-ordinates.

Android Map Zoom Level for Current Location and Destination

I am displaying the user's current position and their intended destination using Google maps, MappingOverlayActivity and ItemizedOverlay.
What is the best way to firstly set the appropriate zoom level so that both locations are displayed on the map on the screen with the maximum area covered by the map, i.e rather than the current location being in the centre, having the centre being in the middle of the 2 points.. Also when the user's current location changes, how can I then recalulate and reset the appropriate zoom level to incorporate the new current location.
You use methods from MapController (which you obtain using mapView.getController()).
Specifically animateTo (or moveTo), and zoomToSpan.
Assuming you have two GeoPoints
GeoPoint current;
GeoPoint destination;
do something like this
mapController.zoomToSpan(Math.abs(current.getLatitudeE6() - destination.getLatitudeE6()), Math.abs(current.getLongitudeE6() - destination.getLongitudeE6());
mapController.animateTo(new GeoPoint((current.getLatitudeE6() + destination.getLatitudeE6())/2, (current.getLongitudeE6() + destination.getLongitudeE6())/2));
so you calculate the span (difference) and center (average) of the points.
For the center of the coordinates you might need to do some math
http://en.wikipedia.org/wiki/Centroid

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

Map view set according to distance

I am a new android developer. I am creating a map view application Where I want to set up my map view according distance from my current location. I have three buttons such as 100m,500m and 1 Km . When application is started then mapview will appear and current location is the center of the map. When i tap on 1 km then the map view is set up 1 km according to current location. How can i do this.Thanks in advance.
You have to compute 1km in latitude and longitude, set the center to your current position ( I guess you succed to do that) and set the span to your MapController:
mController.zoomToSpan((int) spanLat,(int) spanLon);
zoomToSpan
public void zoomToSpan(int latSpanE6,int lonSpanE6)
Attempts to adjust the zoom of the map
so that the given span of latitude and
longitude will be displayed. Because
the zoom can only achieve discrete
levels, and because the aspect ratio
of the map may not match the ratio
given, the quality of the fit may
vary. The only thing we guarantee is
that, after the zoom, at least one of
the new latitude or the new longitude
will be within a factor of 2 from the
corresponding parameter.

Categories

Resources