views:

62

answers:

1

Hey Guys,

How would dynamically draw markers on a Mapview? I've got the following code which will hardcode one point in. I'm looking to pull values from a database..

                 //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);

        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(
            getResources(), R.drawable.pushpin);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 

I've got all the locations in the database. How would I not declare a Point variable etc. for each point the exists in a database table of size N?

Thanks.

+3  A: 

Write an OverlayCalss that implements ItemizedOverlay .This is a better method for implementing multiple markers. You can add the amrkers fetched from the database as :

db = SQLiteDatabase.openDatabase(

            "/data/data/<your project package>/databases/<databasename>", null, 0);

    cursor = db.query(<db table name>, new String[] { <values to fetch> },

            null, null, null, null, null);

    cursor.moveToFirst();

     //Log.i("Log", "Displaying markers through iteration");

    while (cursor.isAfterLast() == false) {

        lat = Double.parseDouble(cursor.getString(2));

        lng = Double.parseDouble(cursor.getString(3));

   //       Log.i("Points", lat + "     " + lng);

        point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        overlayitem = new OverlayItem(point, "", "");

        overlays.addOverlay(overlayitem);

        mapOverlays.add(overlays);

        cursor.moveToNext();

    }

    db.close();

Where overlays is an instance of OverlayCAlss that extends ItemizedOverlay and mapOverlays is a list of Overlay items . Just included the log files for debugging purposes...Decalre the variables used in your file ...

Hope this helps ... :)

ravi