2013-01-31 24 views
6

Sử dụng API Android của Google Maps v2, tôi đang cố gắng đặt các giới hạn của bản đồ bằng cách sử dụng LatLngBounds.Builder() với các điểm từ cơ sở dữ liệu. Tôi nghĩ rằng tôi gần gũi, tuy nhiên các hoạt động đang bị rơi vì tôi không nghĩ rằng tôi đang tải đúng điểm. Tôi có thể chỉ là một vài dòng đi.Giới hạn GoolgeMap thiết lập Android từ cơ sở dữ liệu của các điểm

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

Tôi nghĩ rằng có thể có một lỗi trong làm thế nào tôi sắp xếp cho vòng lặp để có được tất cả những chiếc xe, nếu tôi loại bỏ những điều khoản giới hạn các điểm bản đồ được vẽ corretly như tôi kỳ vọng nhưng không bounding bản đồ đúng cách.

Trả lời

27

bạn đang tạo LatLngBounds.Builder() mọi lúc trong vòng lặp. thử điều này

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)); 
} 
Các vấn đề liên quan