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.
How I did it.
Android - plotting gps coordinate on custom map
Yes you can draw points that aren't on the visible map. Get around this by setting up a LatLngBounds. Then display the map for the LatLngBounds. You can also move the map to display the LatLngBounds. There's nothing tricky here no tangents or slopes or hyperbolic curvature of the earth. All that's taken care of by the point which is in degrees so don't over think your solution. If you turn the map the points will turn with it. So add your points to the map find the bounds and tilt the camera. Tilt the camera back and all the points will still be on screen! This will get you started with the all the points on the screen.
Same as the samples get a reference to a google map.
Let's get started finding the boundry of the map.
This is very late to answer try with this shortest way
First of All get your current location latlongs then your write code for your destination lat longs then calculate distance between your current location and destination location with this code
you can never get same lat longs data because of GPS count 95 meters from your current location to display current location pin or lat long data. either you are same location you can never find exact lat long data this must be differ in points so thats by you need to get distance your current location to your fix lat long data points.
when ever you find "distance" from your destination points to current lat long then fire this code to animate camera and zoom.
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:If the method above does not work
GoogleMap
has it's own listener for layout, you might use that: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