zoom to fit all markers on map google maps v2
Try using this
//Calculate the markers to get their positionLatLngBounds.Builder b = new LatLngBounds.Builder();for (Marker m : markers) { b.include(m.getPosition());}LatLngBounds bounds = b.build();//Change the padding as per neededCameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25,25,5);mMap.animateCamera(cu);
Check this Google Maps v2 library that i have created that includes this functionalityjust use
Marker m1,m2, m3, m4 .... ;mapHelper.zoomToFitMarkers(m1,m2, m3, m4 ....);
EDIT :
Get current location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);//You can use GPS_PROVIDER alsolocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub currentLatitude = location.getLatitude(); currentLongitude = location.getLongitude(); //Add your marker here and zoom to fit it //mapHelper.zoomToFitMarkers(m1,m2, m3, m4,mapHelper.addMarker(currentLatitude,currentLongitude,"Current Location");); } });
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 15));
After many search I have found the most suitable answer:
//LatLngBound will cover all your marker on Google MapsLatLngBounds.Builder builder = new LatLngBounds.Builder();for (int i = 0; i < latLng.size(); i++) { LatLng customMarkerLocation = new LatLng(latLng.get(i).getLat(), latLng.get(i).getLng()); mMap.addMarker(new MarkerOptions().position(customMarkerLocation). icon(BitmapDescriptorFactory.fromBitmap( createCustomMarker(getActivity(), R.drawable.ic_pepsi, "Manish")))).setTitle("iPragmatech Solutions Pvt Lmt"); builder.include(customMarkerLocation); //include Point to be covered } LatLngBounds bounds = builder.build(); int width = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels; int padding = (int) (width * 0.15); // offset from edges of the map 15% of screen // to animate camera with some padding and bound -cover- all markers CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding); mMap.animateCamera(cu);
Reference:Adding custom image in Google Maps Marker, zoom in to keep all markers in the view