java - 如何使用 SQLite Database Databae 在 Map 上填充动态标记?
问题描述
我将纬度和经度存储到我的数据库中,并且我想在地图上填充这些保存位置的标记。
我将数据放入数组列表中,但我不知道如何在地图上填充?
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;
}
}
推荐阅读
- reactjs - 在对 React 应用程序的请求中阅读帖子正文
- python - Python 与 Excel 的集成
- javascript - 当您按下“Enter”时,应在另一个元素的末尾创建一个元素
- flutter - 我使用 ADB 运行颤振应用程序,但该应用程序未显示在菜单中
- amazon-web-services - 无法测试基于 AWS Cognito 的用户注册和登录
- sql-server - SQL Server 到服务总线
- listview - SwiftUI:删除列表视图左右的空间?
- powershell - 在两个非常大的列表中查找不同之处
- node.js - 如何使用 API 路由在 Next.js 上下载文件
- c# - UWP - “System.IO.IOException:'网络访问被拒绝:'......”