I have a list of random latitude and longitude points and I am drawing a route between them. My question is how to bound this route within google map I made below utility method
public static void drawRouteIntoMap(final List<? extends MapHelper> position, final GoogleMap googleMap) {
/*List<MapHelper> position = new ArrayList<MapHelper>();
for (int i = lastPosition; i < maps.size(); i++) {
position.add(maps.get(i));
}*/
if (position.size() > 0 && Validator.isNotNull(googleMap)) {
googleMap.clear();
List<PolylineOptions> polylineOptionses = new ArrayList<PolylineOptions>();
PolylineOptions option = null;
Boolean lastPause = null;
for (MapHelper map : position) {
if (map.isPause()) {
if (Validator.isNull(lastPause) || !lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
} else {
if (Validator.isNull(lastPause) || lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
}
lastPause = map.isPause();
}
for (PolylineOptions options : polylineOptionses) {
googleMap.addPolyline(options);
}
if(Validator.isNotNull(option)){
//List<LatLng> points = option.getPoints();
final LatLngBounds.Builder mapBounds = new LatLngBounds.Builder();
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng startPoint = new LatLng(position.get(0).getLatitude(), position.get(0).getLongitude());
googleMap.addMarker(new MarkerOptions().position(startPoint).title("start").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mapBounds.include(startPoint);
LatLng endPoint = new LatLng(position.get(position.size() - 1).getLatitude(), position.get(position.size() - 1).getLongitude());
mapBounds.include(endPoint);
googleMap.addMarker(new MarkerOptions().position(endPoint).title("finish").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
/* googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.moveCamera(CameraUpdateFactory.zoomOut());*/
}
});
}
}
}
here last pause is boolean indicating whether it is paused point for indicating red color polyline.
but it is not working.Any help is appreciated.
I implemented a similar solution to @rishabh-dutt-sharma with a couple of twists:
This is the code (adapted from my original code, I hope there are no typos):
Probably This would be most simple solution for above mentioned problem, It worked for me very well.
Add all the waypoints to the target under GoogleMapOptions:camera, then it will automatically set the zoom and center the map with respect to Polyline( given Route).
The waypoints( this.wayPoints ) should be in this format:
you can do one thing, find out the distance between 2 latitude and longitude and make check over distance that you got.See,how it work . It's a trick it might useful for you.
Thanks hope this will help you.
You need to include all the points that form the polyline into your
mapBounds
object.Also, you are updating the camera using
animateCamera
and then you are moving it usingmoveCamera
. This second camera update overrides the first one. Just remove the lineThis line is also innecesary (you don't need to move and animate the camera, just one of them):
If you need to zoom out a bit your map you can play with the padding parameter of the
CameraUpdateFactory.newLatLngBounds
method.Try it if it helps you
Try implementing this way
UPDATE
After looking at the image, there are layouts above map. So it would require you to set Variable Padding. You can do it as
Note: While setting variable padding, you can initialize
routePadding = 0
; In your case, approximations are:left = right = 10
,bottom=100
,top=200
. Once set, you can calibrate'em as per your requirement.Recommendation: You can calculate the height of those (top and bottom) layouts in pixels and then set padding accordingly.