I want to set the map zoom level to fit all markers on the map. I have used following method as said by many people, but it is not working for me. It is displaying some other point.
if (positions.size() > 0) {
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : positions) {
builder.include(m.getPosition());
}
builder.include(positions.get(i).getPosition());
}
try {
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(), UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);
}
});
} catch (IllegalStateException e) {
// TODO: handle exception
final View mapView = getSupportFragmentManager()
.findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
// We check which build version we are using.
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver()
.removeGlobalOnLayoutListener(
this);
} else {
mapView.getViewTreeObserver()
.removeOnGlobalLayoutListener(
this);
}
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(),
UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);
}
});
}
});
}
}
}
you can use boundBox to set zoom of the map. Just create one boundbox from google map api and keep adding markers and extend the bound when all markers are added to the boundBox just set the bounbos to map. Map will automatically fit the zoom level.
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.
Manage Your Google Maps ModuleClick on the Google Map menu item, and then click on the Settings tab. You will then see the option to Show all locations. Click to enable this option and Save.
Google Maps has an integer 'zoom level' which is the resolution of the current view. Zoom levels are between 0 (the entire world can be seen on one map) and 21+. You can also set the minimum and maximum values for the zoom parameter.
Try using this
//Calculate the markers to get their position
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : markers) {
b.include(m.getPosition());
}
LatLngBounds bounds = b.build();
//Change the padding as per needed
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25,25,5);
mMap.animateCamera(cu);
Check this Google Maps v2 library that i have created that includes this functionality just 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 also
locationManager.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"););
}
});
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