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.
You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.
The shortcut is simple: just double-tap the maps interface, but instead of lifting your finger after the second tap (the shortcut for zooming in), you leave it touching the screen. Then a swipe up zooms out, and a swipe down zooms in. Neat right?
Google Maps - cannot zoom and move at the same time with animateCamera, but can with moveCamera. Bookmark this question.
First of all, search for a place on Google Maps. After you get the result, switch to the Satellite view by clicking the square icon (with “Satellite” as its caption) on the bottom-left of the map screen. Now zoom in using the + (plus) button present on the right-bottom of the map screen.
Try implementing this way
/**
* Zooms a Route (given a List of LalLng) at the greatest possible zoom level.
*
* @param googleMap: instance of GoogleMap
* @param lstLatLngRoute: list of LatLng forming Route
*/
public void zoomRoute(GoogleMap googleMap, List<LatLng> lstLatLngRoute) {
if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (LatLng latLngPoint : lstLatLngRoute)
boundsBuilder.include(latLngPoint);
int routePadding = 100;
LatLngBounds latLngBounds = boundsBuilder.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, routePadding));
}
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
googleMap.setPadding(left, top, right, bottom);
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.
The reason, why zooming in is not working might be because map has not been inflated yet at the time of calling the method:
moveCamera(com.google.android.gms.maps.CameraUpdate)
Try adding ViewTreeObserver.OnGlobalLayoutListener
to the map:
ViewTreeObserver vto = googleMap.getViewTreeObserver();
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
googleMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
googleMap.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
}
};
vto.addOnGlobalLayoutListener(globalLayoutListener);
If the method above does not work GoogleMap
has it's own listener for layout, you might use that:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.setOnCameraChangeListener(null);
}
});
However, as of play-services-maps 9.4.0 version of the API the method above is deprecated. Use one of:
GoogleMap.OnCameraMoveStartedListener
GoogleMap.OnCameraMoveListener
GoogleMap.OnCameraIdleListener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With