1

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

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) {


}

}

Jorden
  • 708
  • 3
  • 10
  • 20
  • What if you create model class with latitude and longitude, get lat and long from DB and set it in model and add that model in list. Very simple way – M D Sep 24 '19 at 11:55
  • @MD Sir can you suggest me any reference post ? – Jorden Sep 24 '19 at 12:06

1 Answers1

1
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;
    }
}
  • In this code where you are getting data from database ? – Jorden Sep 24 '19 at 13:15
  • I only get data same as question, only change ArrayList type because arraylist not store key,value.If you want then i made custom arraylist without database. – Jatinder Rana Sep 24 '19 at 13:40
  • Thank you so much sir...but I want also see the place name which is also saved in database – Jorden Sep 25 '19 at 06:06
  • googleMap.addMarker(new MarkerOptions().position(alllocation.get(i)) .title("Title")) get address from database and change "Title" string. When you click on marker icon then you able to see your address – Jatinder Rana Sep 25 '19 at 10:32
  • Sir but arrylist type is Latlang ? – Jorden Sep 25 '19 at 10:47
  • java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. – Jorden Sep 25 '19 at 11:58
  • This exception is given – Jorden Sep 25 '19 at 11:58
  • Pls change your query in this line Cursor cursor = db.rawQuery("select latitude,longitude from "+Table_Name_Location,null); Add your database address field in query like this Cursor cursor = db.rawQuery("select address,latitude,longitude from "+Table_Name_Location,null); – Jatinder Rana Sep 25 '19 at 12:15
  • Welcome :) and be happy always and up vote this answer – Jatinder Rana Sep 25 '19 at 13:12
  • Sir I have one query related to map can you give me idea ? – Jorden Sep 30 '19 at 09:39
  • https://stackoverflow.com/questions/58165815/how-can-i-calculate-distance-between-multiple-latitude-and-longitude – Jorden Sep 30 '19 at 11:11