java - 如何使用 SQLite 数据库在 map 上填充动态标记?

标签 java android google-maps google-maps-markers

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

我将数据放入数组列表,但不知道如何填充到 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;
} 

map Activity

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

关于java - 如何使用 SQLite 数据库在 map 上填充动态标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079712/

相关文章:

android - 放置API自动填充问题

Android FusedLocation 的 SettingsApi 不能多次工作

java - for 循环在 Android 设备上非常慢

android - 如何禁用使用 Flutter 1.0 在 Google map 中点击标记时出现的导航按钮?

java - 在 Web GUI 中运行独立的 Java 应用程序

java - 字符在 while 循环中不增加数组计数器?

java - 互联网断开之前的广播接收器

java - 在 Spring JdbcTemplate 中提取输出参数与结果集

android - DrawerLayout setDrawerShadow 和 setDrawerElevation 都不起作用(支持 23.0.1)

java - editText 消息未保存到数据库中