How to get onTap in Polygon in osmdroid?

久未见 提交于 2019-12-24 05:17:33

问题


I have lot's of polygon drawn. Some of which overlaps. I want to be able to detect a tap inside the polygon and display a dialog saying it was clicked inside the polygon (this polygon should be detected on tap). This kind of property can be seen with ItemizedIconOverlay where we can detect all of the OverlayItem with the tap on the OverlayItem. How to implement this on polygon?

The way I draw and add Polygon is:

MyPolygon myNewPolygon = new MyPolygon(this);
myPolygon.setPoints(Polygon.pointsAsCircle(new GeoPoint(38.948714, -76.831918), 20000.0));
map.getOverlays().add(myPolygon);
myNewPolygon.setPoints(Polygon.pointsAsCircle(new GeoPoint(38.851777, -77.037878), 20000.0));
map.getOverlays().add(myNewPolygon);

回答1:


There are various answers, depending on what you want to achieve exactly.

Simple case: if you just want to have a bubble opening, then just set an InfoWindow to your Polygon, as described in the tutorial.

If you want anything else: for Polygons, there is no Listener available, as there are for Markers and Polylines (an enhancement to request, maybe?).

So what you can do is sub-class Polygon - as you did with your MyPolygon. Then override onTap method - or most likely onSingleTapConfirmed - and implement the behaviour you need.

Looking at Polygon.onSingleTapConfirmed source may help (hit test for instance).




回答2:


Have you tried osmbonuspack yet? I know for certain that there's an onclick listener for polygons in it.




回答3:


This is how I'm using it:

class CustomTapPolygon extends Polygon {

    private CustomObject tag;

    public CustomObject getTag() {
        return tag;
    }

    public void setTag(CustomObject tag) {
        this.tag = tag;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e, MapView mapView) {
        if (e.getAction() == MotionEvent.ACTION_UP && contains(e)) {
             // YOUR CODE HERE
             return true;
        }
        return super.onSingleTapUp(e, mapView);
    }
}


来源:https://stackoverflow.com/questions/32977376/how-to-get-ontap-in-polygon-in-osmdroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!