Search code examples
javaandroidgoogle-mapsoverlayclickable

Android: making overlay images clickable


I hope someone can help with this. I have created a map app that puts an icon on locations from an xml file, but I need to make the icons clickable to open a page of info about the location.

At present I have this under the class GPSLocationListener implements LocationListener:

MapOverlay mapOverlay = new MapOverlay();
mapOverlay.setPointToDraw(point, "pointer", null);

List<Overlay> listOfOverLays = mapView.getOverlays();
listOfOverLays.clear();

// doc is data from xml file                
NodeList nodes = doc.getElementsByTagName("result");

listOfOverLays.add(mapOverlay);

for (int i = 0; i < nodes.getLength(); i++) {                       

    Element e = (Element)nodes.item(i);

    String locId = XMLfunctions.getValue(e, "id");
    String aString = XMLfunctions.getValue(e, "lat");
    double lat = Double.parseDouble(aString);
    String bString = XMLfunctions.getValue(e, "long");
    double longi = Double.parseDouble(bString);


    // here, 'point' and mapOverlay have already been created and added                                 
    GeoPoint point2 = new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6));
    MapOverlay mapOverlay2 = new MapOverlay();
    mapOverlay2.setPointToDraw(point2, "place", XMLfunctions.getValue(e, "name"));
    OverlayItem item = new OverlayItem(point2, "", "");
    listOfOverLays.add(mapOverlay2);
    }   

Outside of this class I have a class to create the location icons and assign the location names:

class MapOverlay extends Overlay{
    private GeoPoint pointToDraw;
    private String pointerIcon;
    private String locName;

    public void setPointToDraw(GeoPoint point, String pointer, String locationName){
        pointToDraw = point;
        pointerIcon = pointer;
        locName = locationName;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
        super.draw(canvas, mapView, shadow);

        // convert point to pixels
        Point screenPts = new Point();

        mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        if(pointerIcon == "pointer"){

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pointer);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 22, null);
        }
        else if(pointerIcon == "bar"){
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pointer2);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 15, null);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            paint.setTextSize(20);
            canvas.drawText(locName, screenPts.x, screenPts.y-20, paint);
        }

        return true;
    }
}

I just need to make the image 'pointer2' clickable and open a new page with the 'locId', so that I know which location has been clicked. I have read alot online about doing this, but nothing has worked. Any information that can get me closer to making this work it greatly appreciated.


Solution

  • Sorted it, but pretty much had to start from scratch and use Itemised Overlay. Here is what I referenced: http://blog.chrisblunt.com/android-map-view-double-taps-and-overlay-markers/