In google maps I want to fix my camera to a particular country. I don't need whole worlds Mercator projection. So I need to set LatLngBounds in my google maps. How can I find LatLngBounds of a particular region?
Here is my code but it's not setting LatLngBounds as my expectation set's constraints on right side.
LatLng v1= new LatLng(24.044982389517603,89.75482430309057);
LatLng v2 = new LatLng(22.309956868003045,91.20187237858772);
LatLngBounds BANGLADESH = new LatLngBounds(
v2,v1
);
mMap.setLatLngBoundsForCameraTarget(BANGLADESH);
You can get country boundaries once from openstreetmap.org like described in this article of PÄ“teris Å…ikiforovs or by http://polygons.openstreetmap.fr/get_poly.py?id=184640¶ms=0 and add it as polygon points find LatLngBounds like you do it:
...
Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < N_POINTS; i++) {
builder.include(new LatLng(lat, lng));
}
LatLngBounds BANGLADESH = new LatLngBounds = builder.build();
int padding = 15; // offset from edges of the map in pixels
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(BANGLADESH, padding);
mMap.moveCamera(cameraUpdate );
...
Related
I have two points of LatLng coordinates. And I draw a polyline based on that coordinates . How can I fit that polyline drawing on top of the screen with zoomed out to view full line? Is LatLngBounds.Builder the correct solution for that?
these is my code i am using now
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//latlong and latlong1 are my two points.
String[] latlong = pickUp.split(",");
LatLng location = new LatLng(Double.parseDouble(latlong[0]), Double.parseDouble(latlong[1]));
String[] latlong1 = destination.split(",");
LatLng location1 = new LatLng(Double.parseDouble(latlong1[0]), Double.parseDouble(latlong1[1]));
builder.include(location);
builder.include(location1);
LatLngBounds bounds = builder.build();
int padding = 40;
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.animateCamera(cu);
I am trying to make it so that when I launch my app the phone will zoom in and center to the area in which I have set PolyLines to via an ArrayList of lat and lang points.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<MyPoint> points = Data.getPoints();
PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < points.size(); i++) {
MyPoint point = points.get(i);
polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
}
Polyline polyline = mMap.addPolyline(polylineOptions);
}
I start up the app in genymotion and it zooms in and then centers at the lat/lang ArrayList points I've set.
Currently, I just get a map that shows nearly the whole globe and I have to zoom in manually to the polylines I've set up.
You need the camera update factory class to create a camera update and move the map's camera. Camera Update Factory Documentation
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
ArrayList<MyPoint> points = Data.getPoints();
int padding = 20; // or prefer padding
LatLngBounds.Builder builder = new LatLngBounds.Builder();
PolylineOptions polylineOptions = new PolylineOptions();
for (int i = 0; i < points.size(); i++) {
MyPoint point = points.get(i);
polylineOptions.add(new LatLng(point.getLat(), point.getLng()));
builder.include(new LatLng(point.getLat(), point.getLng()));
}
Polyline polyline = mMap.addPolyline(polylineOptions);
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
}
You need to use LatLngBounds and then zoom your camera to cover all the included markers:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
Use the following line in a loop for all points/markers: (You can use the same loop you are using to add points to polylineOptions.)
builder.include(pointLatLng);
And finally, (outside the loop) -
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
I have cordinates from json and I want to show them all in map , here it shows markers but some of them not appear I want to set zoom to fit appearing all markers in map ,how can I do it ?
JSONArray postsJson = success.getJSONArray("now");
for (int i = 0; i < postsJson.length(); i++) {
Order_Start_Lat=c.getDouble("order_start_latitude");
Order_Start_Lng=c.getDouble("order_start_longitude");
MarkerOptions m = new MarkerOptions()
.title(client_name)
.position(new LatLng(Order_Start_Lat,Order_Start_Lng));
map.addMarker(m);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(Order_Start_Lat,Order_Start_Lng));
LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));
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);
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
I have tryied Like [with 2 Lat Long Details]
final LatLng latlong = new LatLng(lat,lng);
final LatLng latlongcr = new LatLng(currentlat,currentlong);
mapView.addMarker(new MarkerOptions().position(latlongcr).title("Here You Are"));
mapView.addMarker(new MarkerOptions().position(latlong).title(addr));
mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 20));
mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(latlongcr, 20));
mapView.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);
But Get Only One Marker. want to show 2 marker Mapview working fine
Try using LatLngBounds.Builder:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(latlongcr);
builder.include(latlong);
LatLngBounds bounds = builder.build();
mapView.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));