Android Map Zoom Level for Current Location and Destination - android

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

Related

Android Google Map: Calculate radius to be drawn depend on meters and zoom level of map

I would like to known if there is a way to calculate radius to be drawn on Google Map.
Now I have a GoogleMap with ViewOverlay over it for inverting the selection. So I want to know what is actual radius (which to be drawn) equal to.
radiusToByDrawnOnViewOverlay = radiusFromSeekBar * currentGoogleMapZoom
I found my answer here: https://gist.github.com/amay077/6879638. Actually this is method which was deprecated in GoogleMap.

What is difference between ZoomTo and ZoomBy in google map

Currently, I am working with google map and may have confusion between this two methods. I don't differentiate working of this two method so, can anyone explain me what is difference between zoomBy() and zoomTo()
Code:
mMap.animateCamera(CameraUpdateFactory.zoomBy(zoomLevel));
mMap.animateCamera(CameraUpdateFactory.zoomTo(zoomLevel));
CameraUpdateFactory.zoomTo(float) gives you a CameraUpdate that
changes the zoom level to the given value, while keeping all other
properties the same.
CameraUpdateFactory.zoomBy(float) and
CameraUpdateFactory.zoomBy(float, Point) give you a CameraUpdate that
increases (or decreases, if the value is negative) the zoom level by
the given value. The latter fixes the given point on the screen such
that it remains at the same location (latitude/longitude) and so it
may change the location of the camera in order to achieve this.
From official documentation here
So in short zoomTo just changes the zoom level to the given value, while the zoomBy increases or decreases ( e.g. with zoomTo(20) your camera will have the zoom level set to 20, but if your zoom level was 20 and you call zoomBy(-5), your zoom level will become 15)
public static CameraUpdate zoomBy (float amount, Point focus)
Returns a CameraUpdate that shifts the zoom level of the current
camera viewpoint.
A point specified by focus will remain fixed (i.e., it corresponds to
the same lat/long both before and after the zoom process).
This method is different to zoomTo(float) in that zoom is relative to
the current camera.
For example, if the LatLng (11.11, 22.22) is currently at the screen
location (23, 45). After calling this method with a zoom amount and
this LatLng, the screen location of this LatLng will still be (23,
45).
public static CameraUpdate zoomTo (float zoom)
Returns a CameraUpdate that moves the camera viewpoint to a particular
zoom level.

How to put marker at bottom of map instead of center(by default) of map with zoom enabled in android?

In live track app I want to animate camera above marker and want to put marker always at bottom in newLocationChanged.
If I am building CameraPosition without zoom, It works fine.
But with zoom also I need to achieve same.
Use the screen boundaries to get the desired position:
As per documentation:
getBounds()
Return Value: LatLngBounds
Returns the lat/lng bounds of the current viewport. If more than one copy of the world is visible, the bounds range in longitude from -180 to 180 degrees inclusive. If the map is not yet initialized (i.e. the mapType is still null), or center and zoom have not been set then the result is null or undefined.

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

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