如何使用SQLite Database Databae在地图上填充动态标记? [英] How Can I populate dynamic markers on Map using SQLite Database Databae?

查看:69
本文介绍了如何使用SQLite Database Databae在地图上填充动态标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I stored Latitude and Longitude into my Database And I want to populate these saved locations's marker on map.

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

I get data into array list but I have no idea how I populate on 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;
    }
}

这篇关于如何使用SQLite Database Databae在地图上填充动态标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆