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"););
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…