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);
}
});
}
});
}
}
}
Try using this
Check this Google Maps v2 library that i have created that includes this functionality just use
EDIT :
Get current location