Skip to content Skip to sidebar Skip to footer

Adding Marker Google Map Using Data In Database

I'm trying to adding marker to google map using data in database. My table is : CREATE TABLE `markers` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 60 ) NOT

Solution 1:

Create Location Model save infor of Marker include name, Lat, Lng:

Make function getAllMarker from database:

public List<Marker> getAllMarker() {
        List<String> result = newArrayList<>();
        StringselectQuery="SELECT * FROM markers";
        SQLiteDatabasedb= getReadableDatabase();
        Cursorcursor= db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                result.add(newLocation(cursor.getString(cursor.getColumnIndex("name")), cursor.getFloat(cursor.getColumnIndex("lat")), cursor.getFloat(cursor.getColumnIndex("lng"))));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return result;
    }

Add marker to Map:

for(Location l: db.getAllMarker){
        mMap.addMarker(new MarkerOptions().position(new LatLng(l.getLat(), l.getLng())).title(l.getName());
    }

Post a Comment for "Adding Marker Google Map Using Data In Database"