0

Until now, I always draw "points" in the MapActivities with OverlayItems and with a class Point made by me. With this method I can draw some "points" in the MapActivity. But how I can draw "clickable" points ??

I improvise this class Point, and follow a tutorial about the OverlayItems method, but I can implement any method that yourselves explain me for this "clickable points".

Thanks.

joanlopez
  • 579
  • 1
  • 6
  • 17

2 Answers2

0

Have a look at this project as example how to implement clickable OverlayItems https://github.com/jgilfelt/android-mapviewballoons

vasart
  • 6,692
  • 38
  • 39
0

I couldn't find a way to do this so I wrote my own click logic. This is from a project I've put on ice for now and is work in progress (hard coded values for example) but the logic works. Hope it helps:

      @Override
  public boolean onTap(GeoPoint geoPoint, MapView mapView){

      if (!isRoute){ // nothing to do if it's a route

          Cursor cursor = (Cursor) mapView.getTag();
          Projection projection = mapView.getProjection();

          // get pixels for the point clicked
          Point clickedPoint = new Point();
          projection.toPixels(geoPoint, clickedPoint);

          if (cursor.moveToFirst()){
                do {

                    try {
                        Double lat = cursor.getFloat(Database.LAT_COLUMN) * 1E6;
                        Double lng = cursor.getFloat(Database.LONG_COLUMN) * 1E6;
                        GeoPoint thisGeoPoint = new GeoPoint(lat.intValue(), 
                                                             lng.intValue());

                        // get pixels for this point
                        Point overlayPoint = new Point();
                        projection.toPixels(thisGeoPoint,overlayPoint);

                        // did the user click within 30 pixels?
                        if ((Math.abs(clickedPoint.x - overlayPoint.x) < 30) && (Math.abs(clickedPoint.y - overlayPoint.y) < 30)){

                            // get a cursor to this record
                            Cursor thisCursor = TestApp.db.rawQuery("SELECT * FROM " + Database.DATABASE_TABLE_PINS + " WHERE CID='" + cursor.getString(Database.ID_COLUMN) + "'", null);
                            thisCursor.moveToFirst();

                            // create and show an instance of the PinDetailsDialog
                            PinDetailsDialog customiseDialog ;
                            // TODO this is a kludge, why does this throw an exception sometimes?
                            try{
                                customiseDialog = new PinDetailsDialog(mapView, context,thisCursor,context.getResources().getConfiguration().orientation);
                                customiseDialog.show(); 
                            } catch (Exception e){
                                customiseDialog = new PinDetailsDialog(mapView, mapView.getContext(),thisCursor,context.getResources().getConfiguration().orientation);
                                customiseDialog.show(); 
                            }               

                            return true;
                        }


                    } catch (Exception e){
                        e.printStackTrace();
                    }

                } while(cursor.moveToNext());       
          }

      }
      return true;

  }

The basic idea is to get the point where the user clicked on the map, convert the point into lat long then iterate my data points looking for a match. Note that I am using a hit test of +/- 30 pixels (which I will not hard code when I pick this up again.

I left the TODO in there just in case you stumble into something similar but I do suspect it's entirely down to a problem somewhere in my PinDetailsDialog class implementation.

I use multiple map views, each of which uses data stored in a SQLite table. I store a reference to a cursor to read the data in the tag property of the Mapview, hence the .getTag() call.

Simon
  • 14,407
  • 8
  • 46
  • 61