2013-01-31 8 views
6

za pomocą mapy Google Android API v2, Próbuję ustawić granice na mapie za pomocą LatLngBounds.Builder() z punktów z bazy danych. Myślę, że jestem blisko, jednak aktywność jest upaść, bo nie sądzę, jestem właściwie ładowanie punktów. Mogę być tylko kilka wierszy dalej.Android granice zestaw GoolgeMap od z bazy punktów

//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)); 
    } 
} 

myślę, że może być błąd w jaki sposób ułożone pętli for, aby wszystkie samochody, jeśli usunąć wypowiedzi granic punkt na mapie wykreślono corretly jak się spodziewałem, ale nie ograniczające mapę poprawnie.

Odpowiedz

27

tworzysz nowy LatLngBounds.Builder() za każdym razem w pętli. spróbować

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)); 
} 
Powiązane problemy