Java 如何在单个latlng arraylist中设置纬度和经度arraylist,然后显示latlng坐标列表

Java 如何在单个latlng arraylist中设置纬度和经度arraylist,然后显示latlng坐标列表,java,android,sqlite,arraylist,google-maps-markers,Java,Android,Sqlite,Arraylist,Google Maps Markers,我一直在努力从sqlite数据库中提取标记。到目前为止,我已经了解到应用程序使用for循环显示数据库中的最后一个标记,但我不确定如何显示所有位置。我的猜测是,应用程序通过位置纬度的for循环和位置经度的for循环,但只存储最后的变量。我知道我正在尝试将arraylist存储在变量中,然后尝试将变量存储在LatLng arraylist中,我不知道如何存储从位置纬度和位置经度到LatLng arraylist的所有值 这是我试图显示位置的地图活动 MapsActivity.java pri

我一直在努力从sqlite数据库中提取标记。到目前为止,我已经了解到应用程序使用for循环显示数据库中的最后一个标记,但我不确定如何显示所有位置。我的猜测是,应用程序通过位置纬度的for循环和位置经度的for循环,但只存储最后的变量。我知道我正在尝试将arraylist存储在变量中,然后尝试将变量存储在LatLng arraylist中,我不知道如何存储从位置纬度和位置经度到LatLng arraylist的所有值

这是我试图显示位置的地图活动 MapsActivity.java


   private GoogleMap mMap;
   TextView viewResult;
   MyDatabaseHelper myDatabaseHelper;
   Double latitude = null;
   Double longitude = null;
   ArrayList<String> location_id, location_title;
   ArrayList<Double> location_latitude, location_longitude;
   ArrayList<LatLng> locations;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_maps);
       // Obtain the SupportMapFragment and get notified when the map is ready to be used.
       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
               .findFragmentById(R.id.map);
       mapFragment.getMapAsync(this);


       //Initialize variables
       myDatabaseHelper = new MyDatabaseHelper(MapsActivity.this);
       location_id = new ArrayList<>();
       location_title = new ArrayList<>();
       location_latitude = new ArrayList<>();
       location_longitude = new ArrayList<>();
       locations = new ArrayList<>();

       storeDataInArrays();

       //Set arraylist containing latitude into a variable
       for (int i = 0; i < location_latitude.size(); i++) {

           latitude = (Double.valueOf(location_latitude.get(i)));
       }

       //Set arraylist containing longitude into a variable
       for (int i = 0; i < location_longitude.size(); i++) {

           longitude = (Double.valueOf(location_longitude.get(i)));
       }
       //add latitude and longitude variables into locations arraylist
       locations.add(new LatLng(latitude, longitude));
   }

   @Override
   public void onMapReady(GoogleMap googleMap) {
       mMap = googleMap;

       // Add a marker in Sydney and move the camera
       LatLng sydney = new LatLng(-34, 151);
       mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
       mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));


       //Display markers from locations arraylist
       for(LatLng location : locations){
           mMap.addMarker(new MarkerOptions()
           .position(location)
           .title("marker"));
       }
   }

   //Get data from database and store in arrays
   void storeDataInArrays() {
       Cursor cursor = myDatabaseHelper.readAllData();
       if (cursor.getCount() == 0) {
           Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
       } else {
           while (cursor.moveToNext()) {
               location_id.add(cursor.getString(0));
               location_title.add(cursor.getString(1));
               location_latitude.add(cursor.getDouble(2));
               location_longitude.add(cursor.getDouble(3));

           }

       }

   }

}

发布代码的最简单方法是修改
storeDataInArrays
方法,直接填充
位置
列表:

while (cursor.moveToNext()) {
    location_id.add(cursor.getString(0));
    location_title.add(cursor.getString(1));

    locations.add(new LatLng(cursor.getDouble(2),cursor.getDouble(3)));

}
然后在
storedatainarray
之后删除
onCreate
中的代码:

    // .. snippet from onCreate
    storeDataInArrays();

    // remove code loops etc.
}
此外,尽管这在代码中不是问题,但值得注意的是,
onMapReady
是异步的(但在UI线程上调用),因此在调用
mapFragment.getmapsync(此)之前调用
storedatainarays
可能更清晰。因为
onMapReady
取决于已填充的位置。(从技术上讲,它总是在后面调用,因为它将位于同一个活套上。)


在某些情况下,您可能会意识到希望将所有位置信息封装在一起(id、title、lat、lng),其中一种方法是在活动类中创建一个私有类,并将其用作列表类型,如中所示:

private static class MyLatLngData {
   private String id;
   private String title;
   private LatLng latlng;
   public MyLatLngData(String id, String title, double lat, double lng) {
       this.id = id;
       this.title = title;
       this.latlng = new LatLng(lat,lng);
   }
   // add getters and setters or make fields public....e.g.
   public LatLng getLatLng() { return latlng; }
   public void setLatLng(LatLng latlng) { this.latlng = latlng; }
   public String getTitle() { return title; }
  // add others...
}

您的
storeDataInArrays
将成为:

while (cursor.moveToNext()) {
    // remove all previous list adds.
    locations.add(new MyLatLngData(cursor.getString(0), cursor.getString(1), cursor.getDouble(2), cursor.getDouble(3));

}
//Display markers from locations arraylist
for(MyLatLngData location : locations){
    mMap.addMarker(new MarkerOptions()
    .position(location.getLatLng())
    .title("marker"));  // here you could use location.getTitle()
}
位置
定义如下:

ArrayList<MyLatLngData> locations;

太棒了,非常感谢!非常清楚,信息丰富。我花了相当长的时间来完成这项工作,到目前为止我已经完成了,但是你的贡献确实帮助了很多。
//Display markers from locations arraylist
for(MyLatLngData location : locations){
    mMap.addMarker(new MarkerOptions()
    .position(location.getLatLng())
    .title("marker"));  // here you could use location.getTitle()
}