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

问题描述

我一直在努力从sqlite数据库中绘制标记。到目前为止,到目前为止,该应用程序使用for循环显示数据库中的最后一个标记,但是我不确定如何显示所有位置。我的猜测是,应用程序经过location_latitude的for循环和location_longitude的for循环,但仅存储最后一个变量。我了解到,我试图将一个arraylist存储在一个变量中,然后再将该变量存储在LatLng Arraylist中,我不知道如何存储从location_latitude和location_longitude到LatLng arraylist的所有值。

这是MapsActivity,此处我试图显示位置 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));

           }

       }

   }

}

这是数据库助手的读取所有数据的方法

 //readalldata method 
    public Cursor readAllData() {
        String query1 = "SELECT * FROM " + TABLE_NAME;
        sqliteDatabase sqliteDatabase = this.getReadableDatabase();

        Cursor cursor = null;
        if(sqliteDatabase != null) {
            cursor = sqliteDatabase.rawQuery(query1,null);
        }
        return cursor;

    }


解决方法

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

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

}

然后在onCreate之后消除{​​{1}}中的代码:

storeDataInArrays

此外,尽管这不是代码中的问题,但值得注意的是 // .. snippet from onCreate storeDataInArrays(); // remove code loops etc. } 是异步的(但在UI线程上被调用),因此在调用{{ 1}}。由于onMapReady取决于已经填充的位置。 (从技术上讲,它总是在同一个弯针上才被调用。)


在某个时候,您可能会意识到您希望将所有位置信息封装在一起(id,title,lat,lng),而实现此目的的一种方法是在活动类中创建一个私有类,并将其用作列表键入:

storeDataInArrays

}

您的mapFragment.getMapAsync(this);将变为:

onMapReady

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

}