简体   繁体   中英

OSMDROID - longPressHelper only fires the first time when it is used to add a marker

I'm using osmdroid and have implemented a MapEventsReceiver, MapEventsOverlay and a LongPressHelper in order to add a new marker when the user holds down on the map. This works the first time and the first time only.

If I remove the addMarker(p); from my longPressHelper then it will fire every single time.

Has anyone got any idea why this is happening like this?

code:

mapviewInit - called in onCreate private void mapviewInit() {

    mapview = (MapView) findViewById(R.id.mapview);
    mapview.setTileSource(TileSourceFactory.MAPNIK);
    mapview.setBuiltInZoomControls(true);
    mapview.setMultiTouchControls(true);

    IMapController mapController = mapview.getController();
    mapController.setZoom(16);
    GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
    mapController.setCenter(startPoint);

    MapEventsReceiver meReceiver = new MapEventsReceiver() {

        @Override
        public boolean singleTapConfirmedHelper(GeoPoint p) {
            return false;
        }

        @Override
        public boolean longPressHelper(GeoPoint p) {

            Toast toast = Toast.makeText(getApplicationContext(), "DEBUGDEBUGDEBUG", Toast.LENGTH_LONG);
            toast.show();

            addMarker(p);



            return true;
        }
    };

addMarker - called by LongPressHelper public void addMarker(GeoPoint geoPoint) {

    Drawable dr = getResources().getDrawable(R.drawable.icn_crosshair_red);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 100, 100, true));

    mapview.getOverlays().clear();
    mapview.invalidate();

    selectedPosMarker = new Marker(mapview);
    selectedPosMarker.setPosition(geoPoint);

    selectedPosMarker.setInfoWindow(null);

    selectedPosMarker.setIcon(d);
    selectedPosMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);


    mapview.getOverlays().add(selectedPosMarker);
    mapview.invalidate();
}

I assume you add a MapEventsOverlay somewhere, in a part of your code you didn't provided. The issue is that you remove it in addMarker! :

mapview.getOverlays().clear();

So of course it is not present to react to long press on the second time...

Don't call mapview.invalidate() twice in the same method, it's useless and time-consuming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM