2013-01-31 10 views
6

Utilizzo dell'API Android di Google Maps v2, Sto cercando di impostare i limiti della mappa utilizzando LatLngBounds.Builder() con i punti da un database. Penso di essere vicino, tuttavia l'attività si sta bloccando perché non penso di caricare correttamente i punti. Potrei essere solo un paio di righe di distanza.Android set Limitazioni GoolgeMap dal database dei punti

//setup map 
private void setUpMap() { 

    //get all cars from the datbase with getter method 
    List<Car> K = db.getAllCars(); 

    //loop through cars in the database 
    for (Car cn : K) { 

     //add a map marker for each car, with description as the title using getter methods 
     mapView.addMarker(new MarkerOptions().position(new LatLng(cn.getLatitude(), cn.getLongitude())).title(cn.getDescription())); 

     //use .include to put add each point to be included in the bounds 
     bounds = new LatLngBounds.Builder().include(new LatLng(cn.getLatitude(), cn.getLongitude())).build(); 

     //set bounds with all the map points 
     mapView.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50)); 
    } 
} 

penso che ci può essere un errore nel modo in cui ho organizzato il ciclo for per ottenere tutte le auto, se mi tolgo la dichiarazioni delimita il punto della mappa vengono tracciati corretly come mi aspettavo, ma non delimita la mappa correttamente.

risposta

27

si sta creando un nuovo LatLngBounds.Builder() ogni volta nel ciclo. prova questo

private LatLngBounds.Builder bounds; 
//setup map 
private void setUpMap() { 

    bounds = new LatLngBounds.Builder(); 
    //get all cars from the datbase with getter method 
    List<Car> K = db.getAllCars(); 

    //loop through cars in the database 
    for (Car cn : K) { 

     //add a map marker for each car, with description as the title using getter methods 
     mapView.addMarker(new MarkerOptions().position(new LatLng(cn.getLatitude(), cn.getLongitude())).title(cn.getDescription())); 

     //use .include to put add each point to be included in the bounds 
     bounds.include(new LatLng(cn.getLatitude(), cn.getLongitude())); 


    } 
    //set bounds with all the map points 
    mapView.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50)); 
} 
Problemi correlati