fit full world map on android screen - android

I am working on an Android app and I have two markers i want to place on a google map. The idea is that the user can see the two locations at a glance without having to interact with the map.
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(userPos);
b.include(cardPos);
LatLngBounds bounds = b.build();
int width = (int) (0.7 * infoView.getWidth());
int height = (int) (0.7 * mapView.getHeight());
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, 5);
googleMap.animateCamera(cu);
The code works fine when the two locations are not very far apart. But when the locations are, for example, Singapore and USA, the two markers cannot be seen together.
I have tried to manually set the zoom level to 0 with the same result.
Is there any way that I can show the entire world map on the android device at once (i.e without the need for scrolling on the user's part)?
Update:
I have tried setting the map to zoom level 0 explicitly. The map does not fit the View I have created. Is it not possible to have the full world view of the map on the screen?

You should use the CameraUpdate class to do (probably) all programmatic map movements.
To do this, first calculate the bounds of all the markers like so:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
Then obtain a movement description object by using the factory: CameraUpdateFactory:
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
Finally move the map:
googleMap.moveCamera(cu);
Or if you want an animation:
googleMap.animateCamera(cu);
That's all :)

Related

How to show only a specific region/area in google map for android api v2 [Android]

How to show only a specific region/area in google map for android api v2?.all functionalities must be available in that region like zooming,dragging etc.
the all other areas must be cropped off or invisible. Is that possible???
I am using eclipse.help me please
This trick worked for me
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//get latlong for corners for specified place
LatLng one = new LatLng(27.700769, 85.300140);
LatLng two = new LatLng(27.800769, 85.400140);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//add them to builder
builder.include(one);
builder.include(two);
LatLngBounds bounds = builder.build();
//get width and height to current display screen
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
// 20% padding
int padding = (int) (width * 0.20);
//set latlong bounds
mMap.setLatLngBoundsForCameraTarget(bounds);
//move camera to fill the bound to screen
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));
//set zoom to level to current so that you won't be able to zoom out viz. move outside bounds
mMap.setMinZoomPreference(mMap.getCameraPosition().zoom);
}
To show only specific area or region then you can use Polygons.
Polygon objects are similar to Polyline objects in that they consist of a series of coordinates in an ordered sequence. However, instead of being open-ended, polygons are designed to define regions within a closed loop with the interior filled in.
You can add a Polygon to the map in the same way as you add a Polyline. First create a PolygonOptions object and add some points to it. These points will form the outline of the polygon. You then add the polygon to the map by calling GoogleMap.addPolygon(PolygonOptions) which will return a Polygon object.
Visit this page on how to use Polygon.

Getting particular location of google map in center in android studio

I used this code to get particular location in center.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(33.7167, 73.0667), 4))
It worked somehow but after putting 30 no of markers simultaneously. I didn't get location in center.
this is my snippet of code
http://i.stack.imgur.com/jvATG.png
http://i.stack.imgur.com/RPsNx.png
http://i.stack.imgur.com/ImmD8.png
http://i.stack.imgur.com/a930b.png
You can update camera to the center of bounds of markers. For that -
Create LatLngBounds.Builder object and add all the markers in it.
LatLngBounds.Builder builder = new LatLngBounds.Builder();
While adding marker store it to variable like
Marker marker1 = mMap.addMarker(....);
Marker marker2 = mMap.addMarker(....);
Then add all markers in it using
builder.include(marker1.getPosition());
builder.include(marker2.getPosition());
Then build LatLngBounds
LatLngBounds bounds = builder.build();
Then create CameraUpdate and animate camera to relative position
int padding = 0; // offset from edges of the map in pixels
CameraUpdate camUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(camUpdate);
And done.
Refer this answer for more details.
Hope it'll work.

Fix zoom level between two Marker in Google-Map

Currently I work with Google-Map-v2 and I want to show Direction between two Markers. Everything is ok and direction between two Markers calculate and draw fine.But only one problem is remain.
The problem is Zoom level between these two markers in Google-Map is too much. I Search in SO and find a solution for changing Zoom Level by Following code :
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(dest);
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 50);
mMap.animateCamera(cu);
Now it's better But still zoom level is not good. Is it possible to do some fixes on Code ? ( if I can some padding to view is fixed )
I Also change 50 to 6,12,... But nothing changed.
You are using the correct code, and the value (50) needs to be increased if you want to zoom less (be outer) or decreased to 0 if you want to be in the smallest area containing the two markers (you can skip the value in case).
If you set a value of 150 or more, and the level is too much you can use the animation callback to zoom out after the LatLng zoom:
Google API
com.google.android.gms.maps.GoogleMap.CancelableCallback)
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(origin);
builder.include(dest);
LatLngBounds bounds = builder.build();
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 50);
mMap.animateCamera(cu, new GoogleMap.CancelableCallback(){
void onCancel(){}
void onFinish(){
CameraUpdate zout = CameraUpdateFactory.zoomBy(-3.0);
mMap.animateCamera(zout);
}
});
This should zoom to the LatLng and when finished, zoom back of 3 levels.
try this:
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(yourlatlng)
// .bearing(targetBearing)// you can ignore it
.zoom(12)// your zoom value
// .tilt(40) // angle of view
.build();
mMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
500,
null);

Android - GoogleMapAPI v2 marker icons out of bounds

actually I'm working on an android application using the Google Map APIv2, so far I'm trying to display some markers on a the map, also i want to have all the markers inside my camera view, for this I'm using
LatLngBounds.Builder builder = new LatLngBounds.Builder();
LatLngBounds bounds;
int padding = 0;
//markMap is a hashmap populated with my markers positions.
HashMap<String, LatLng> markMap = new HashMap<String, LatLng>();
for (Entry<String, LatLng> entry : markMap.entrySet())
{
map.addMarker(new MarkerOptions().position(entry.getValue()));
builder.include(entry.getValue());
}
bounds = builder.build();
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.animateCamera(update);
This works perfectly fine, but there is only one problem with it, and that is : my current camera zoom level only includes the marker points, but the marker icon itself is usually, totally or partially, out of bounds.
Any suggestions on what i should do so i can have a the markers icons included inside my camera view ?
Why not just add some padding:
CameraUpdate update = CameraUpdateFactory.newLatLngBounds(bounds, 50);
I don't see why CameraUpdate should take into consideration the width of the marker itself. It includes the coordinates not the marker itself.
Another option could be:
CameraPosition cameraPosition =
new CameraPosition.Builder().target(ll).zoom(16).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
You can see more about "Camera Position" here

adjust map to make 2 points visible

I have 2 LatLng, usually a few kilometers apart, and I want to zoom map such that both points are visible. My code below sometimes works, sometimes not (map is zoomed to level 3 then). I think the reason is that the loop is too fast for the map to make the necessary adjustments in time.
Is there a way to wait for the map to update before making the test again? To note that process runs on UI thread
double x1=from.latitude;
double x2= to.latitude;
double x1half=(x1+x2)/2;
x1=from.longitude;
x2= to.longitude;
double x2half=(x1+x2)/2;
LatLng pp=new LatLng(x1half, x2half);
map.moveCamera(CameraUpdateFactory.newLatLng(pp));//3.0
//now zoom map so that both points are in
double zoom=21;//V3.3
while(zoom>2){//
map.animateCamera( CameraUpdateFactory.zoomTo( (float) zoom ) );
if(isCurrentLocationVisible(from,map)&&isCurrentLocationVisible(to,map)) return;
/now we know map is still not visible. give it a bit more room
zoom--;
}
I'd advice you to use the newLatLngBounds() instead..You can create your bounds from your two geo points with the LatLngBounds.builder().
To the second part of your question..You could do somethink like map.post(new Runnable..) which could solve the concurrency issue.
You can use LatLngBounds like the following:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(locationOne);
builder.include(locationTwo);
LatLngBounds bounds = builder.build();
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,screen width, screen height, padding);
map.moveCamera(cu);

Categories

Resources