Reputation: 45
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 NULL ,
`lat` FLOAT( 10, 6 ) NOT NULL ,
`lng` FLOAT( 10, 6 ) NOT NULL
)
I want to add marker in the google map without using :
final LatLng Hospital = new LatLng(21 , 57);
Marker Hospital= googleMap.addMarker(new MarkerOptions().position(Hospital ).title("Hospital"));
how to add markers for all the locations that are stored in the database?
Upvotes: 0
Views: 3838
Reputation: 4548
Create Location
Model save infor of Marker include name, Lat, Lng
:
Make function getAllMarker from database:
public List<Marker> getAllMarker() {
List<String> result = new ArrayList<>();
String selectQuery = "SELECT * FROM markers";
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
result.add(new Location(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());
}
Upvotes: 2