0

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?

Kancil
  • 35
  • 3
  • 10

1 Answers1

2

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());
    }
mdtuyen
  • 4,470
  • 5
  • 28
  • 50
  • I mean MySQL, not SQLite. Whether the same way? or adding a php file on the server? – Kancil Nov 27 '15 at 09:34
  • you need write an api to get list all marker from server (using mySQL), location you will parser data to put it to a List – mdtuyen Nov 27 '15 at 09:44
  • I'm sorry. I'm a beginer programmer. Would you tell me how to send all data locations in database to android (from file .php) ? – Kancil Nov 27 '15 at 10:28
  • Pls read this example to understand about information between server and client: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ – mdtuyen Nov 27 '15 at 11:49