如何在单个 latlng arraylist 中设置纬度和经度 arraylist,然后显示 latlng 坐标列表
How to set a latitude and a longitude arraylist in a single latlng arraylist and then display list of latlng coordinates
我一直致力于从 sqlite 数据库中获取标记。到目前为止,我已经知道应用程序使用 for 循环显示数据库中的最后一个标记,但我不确定如何显示所有位置。我的猜测是应用程序通过 location_latitude 的 for 循环和 location_longitude 的 for 循环,但只存储最后一个变量。我知道我正在尝试将数组列表存储在变量中,然后尝试将变量存储在 LatLng Arraylist 中,我不知道如何将 location_latitude 和 location_longitude 中的所有值存储到LatLng 数组列表。
这是我试图显示位置的 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)));
}
然后去掉storeDataInArrays
后onCreate
中的代码:
// .. snippet from onCreate
storeDataInArrays();
// remove code loops etc.
}
此外,虽然这在您的代码中不是问题,但值得注意的是 onMapReady
是异步的(但在 UI 线程上调用)因此调用 [=16 可能更清楚=] 在调用 mapFragment.getMapAsync(this);
之前。由于 onMapReady
取决于已经填充的位置。 (从技术上讲,它总是会在之后被调用,因为它会在同一个循环器上。)
在某些时候,您可能会意识到您想要将所有位置信息(id、标题、lat、lng)封装在一起,一种方法是在您的 [=43] 中创建一个私有 class =] class 并使用它作为列表类型,如 :
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));
}
locations
定义如下:
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()
}
我一直致力于从 sqlite 数据库中获取标记。到目前为止,我已经知道应用程序使用 for 循环显示数据库中的最后一个标记,但我不确定如何显示所有位置。我的猜测是应用程序通过 location_latitude 的 for 循环和 location_longitude 的 for 循环,但只存储最后一个变量。我知道我正在尝试将数组列表存储在变量中,然后尝试将变量存储在 LatLng Arraylist 中,我不知道如何将 location_latitude 和 location_longitude 中的所有值存储到LatLng 数组列表。
这是我试图显示位置的 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)));
}
然后去掉storeDataInArrays
后onCreate
中的代码:
// .. snippet from onCreate
storeDataInArrays();
// remove code loops etc.
}
此外,虽然这在您的代码中不是问题,但值得注意的是 onMapReady
是异步的(但在 UI 线程上调用)因此调用 [=16 可能更清楚=] 在调用 mapFragment.getMapAsync(this);
之前。由于 onMapReady
取决于已经填充的位置。 (从技术上讲,它总是会在之后被调用,因为它会在同一个循环器上。)
在某些时候,您可能会意识到您想要将所有位置信息(id、标题、lat、lng)封装在一起,一种方法是在您的 [=43] 中创建一个私有 class =] class 并使用它作为列表类型,如 :
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));
}
locations
定义如下:
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()
}