Add marker to map on tap

谁说我不能喝 提交于 2019-12-07 16:32:41

问题


I'm coding an application using osmdroid to show a map and i would like to add a marker to it when user taps over map, actually i have been able to do this usin motion event but this adds a marker even when user is zooming in or out the map i don't want this. This is the code i have to add the marker:

@Override    
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int actionType = ev.getAction();
        switch (actionType) {
        case MotionEvent.ACTION_DOWN:
                Projection proj = myOpenMapView.getProjection();
                GeoPoint loc = (GeoPoint) proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
                String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
                String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
                List<OverlayItem> anotherOverlayItemArray = new ArrayList<OverlayItem>();
                ExtendedOverlayItem mapItem = new ExtendedOverlayItem("", "", new GeoPoint((((double)loc.getLatitudeE6())/1000000), (((double)loc.getLongitudeE6())/1000000)), this);
                mapItem.setMarker(this.getResources().getDrawable(R.drawable.marker));
                anotherOverlayItemArray.add(mapItem);
                ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay 
                 = new ItemizedIconOverlay<OverlayItem>(
                   this, anotherOverlayItemArray, null);
                myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
                Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
                toast.show();

        }
        return super.dispatchTouchEvent(ev);
    }

This is how i finally solved it:

Overlay touchOverlay = new Overlay(this){
                    ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = null; 
                    @Override
                    protected void draw(Canvas arg0, MapView arg1, boolean arg2) {

                    }
                    @Override
                    public boolean onSingleTapConfirmed(final MotionEvent e, final MapView mapView) {
                        Projection proj = mapView.getProjection();
                        GeoPoint loc = (GeoPoint) proj.fromPixels((int)e.getX(), (int)e.getY()); 
                        longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
                        latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);
                        ArrayList<OverlayItem> overlayArray = new ArrayList<OverlayItem>();
                        OverlayItem mapItem = new OverlayItem("", "", new GeoPoint((((double)loc.getLatitudeE6())/1000000), (((double)loc.getLongitudeE6())/1000000)));
                        mapItem.setMarker(marker);
                        overlayArray.add(mapItem);
                        if(anotherItemizedIconOverlay==null){
                            anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
                            myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
                            myOpenMapView.invalidate();
                        }else{
                            myOpenMapView.getOverlays().remove(anotherItemizedIconOverlay);
                            myOpenMapView.invalidate();
                            anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
                            myOpenMapView.getOverlays().add(anotherItemizedIconOverlay);
                        }
                        dlgThread();
                        return true;
                    }
                };
                myOpenMapView.getOverlays().add(touchOverlay);

回答1:


You should create an Overlay and override the onSingleTapConfirmed() method to get single-taps:

@Override
public boolean onSingleTapConfirmed(final MotionEvent event, final MapView mapView) {
    // Handle single-tap here, then return true.
    return true;
}


来源:https://stackoverflow.com/questions/18613992/add-marker-to-map-on-tap

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