如何使用 SQLite 数据库 Databae 在地图上填充动态标记?



我将纬度和经度存储到我的数据库中,我想在地图上填充这些保存的位置的标记。

我将数据放入数组列表,但我不知道如何在地图上填充?

ArrayList<Double> get_location(){
ArrayList<Double> location = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select latitude,longitude from "+Table_Name_Location,null);
if(cursor.getCount() > 0){
while (cursor.moveToNext()) {                                  
Double latitude = cursor.getDouble(cursor.getColumnIndex(Latitude));
Double longitude = cursor.getDouble(cursor.getColumnIndex(Longitude));
location.add(latitude);
location.add(longitude);
}
}
cursor.close();
return location;
} 

地图活动

public class Visited_Places extends FragmentActivity implements OnMapReadyCallback {
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visited__places);
databaseHelper = new DatabaseHelper(this);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.maps);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {

}

}

public class Visited_Places extends FragmentActivity implements OnMapReadyCallback {
DatabaseHelper databaseHelper;
ArrayList<HashMap<String, Object>> alllocation = new ArrayList<HashMap<String, Object>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visited__places);
databaseHelper = new DatabaseHelper(this);
alllocation = get_location();
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.maps);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
//put multiple location map
if (googleMap != null) {
for (int i = 0; i < alllocation.size(); i++) {
HashMap<String, Object> hash = alllocation.get(i);
Double lat = (Double) hash.get("latitude");
Double lang = (Double) hash.get("longitude");
String address = hash.get("address").toString();
if (i == 0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lang), 14));
}
googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lang))
.title(address));

}
}

}
//getting location from database  
ArrayList<HashMap<String, Object>> get_location(){
ArrayList<HashMap<String, Object>> location = new ArrayList<HashMap<String, Object>>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select address,latitude,longitude from "+Table_Name_Location,null);
if(cursor.getCount() > 0){
while (cursor.moveToNext()) {
Double latitude = cursor.getDouble(cursor.getColumnIndex(Latitude));
Double longitude = cursor.getDouble(cursor.getColumnIndex(Longitude));
//get your address
String address = cursor.getString(cursor.getColumnIndex(Address));
HashMap<String, Object> hash = new HashMap<String, Object>();
hash.put("latitude", latitude);
hash.put("longitude", longitude);
hash.put("address", address);
location.add(hash);
}
}
cursor.close();
return location;
}
}

相关内容

  • 没有找到相关文章

最新更新