0
我想實現適合所有標記點的縮放,這些標記點來自數據庫,我製作了一個標記數組,但是當我運行應用程序只給我一個標記放大,我在這裏錯過了什麼?縮放以適應地圖上的所有標記谷歌地圖v2 android
public class MapaViagem extends FragmentActivity {
private GoogleMap googleMap;
private String rm_IdViagem;
private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
private ViagemModel mViagemModel = new ViagemModel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
try {
Bundle parametros = getIntent().getExtras();
rm_IdViagem = parametros.getString("id_viagem");
Repositorio ca = new Repositorio(this);
mViagemModel = ca.getViagemPorId(Integer.valueOf(rm_IdViagem));
Repositorio cl = new Repositorio(this);
mClienteModel = cl.getClientesViagem(Integer.valueOf(rm_IdViagem));
for (int i = 0; i < mClienteModel.size(); i++) {
Repositorio mRepositorio = new Repositorio(this);
EnderecoModel mEnderecoModel = mRepositorio.getEnderecosDoCliente(Integer.valueOf(mClienteModel.get(i).getClientes_id()));
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing/hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable/Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable/Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable/Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable/Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable/Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
float latitude = Float.parseFloat(mEnderecoModel.getLatitude());
float longitude = Float.parseFloat(mEnderecoModel.getLongitude());
// before loop:
List<Marker> markers = new ArrayList<Marker>();
Marker marker = googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)));
markers.add(marker);
markers.size();
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : markers) {
builder.include(m.getPosition());
}
LatLngBounds bounds = builder.build();
final CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25, 25, 0);
googleMap.animateCamera(cu);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Não foi possível carregar o mapa", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}