2013-01-31 3 views
6

Google Maps Android API v2を使用して、LatLngBounds.Builder()を使用してデータベースのポイントを使用して地図の境界を設定しようとしています。私は近くにいると思うが、私はポイントを適切に読み込んでいるとは思わないので、アクティビティがクラッシュしている。私は数行離れているかもしれません。Androidの設定点のデータベースからのGoolgeMapの境界

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

は、私は私は私が期待されるが、適切にマップの境界ではないようなマップのポイントがcorretlyプロットされた境界文を削除する場合は、forループ、すべての車を取得するように構成された方法に誤りがあるかもしれないと思います。

答えて

27

ループ内に毎回新しいLatLngBounds.Builder()を作成しています。 試着する

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)); 
} 
関連する問題