Using onTouchListener not returning desired values

1423
2
03-28-2017 07:58 AM
AlejandroPallares
New Contributor II

The company now wants me to add a function to allow users to create a mark on a pressed location in the MapView. I've been trying using all the X,Y functions from the both parameters View and MotionEvent but none returns the latitude and longitude i require.

This function is located inside the onCreate function of the Activity

mMapView.setOnTouchListener(new MapView.OnTouchListener() {
    @Override
    public boolean onMultiPointerTap(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTouchDrag(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onRotate(MotionEvent motionEvent, double v) {
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        return false;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return false;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

         //Already tried using getRawX and getRawY, none works
        latitude = event.getX();
        longitude = event.getY();

        Log.i("Location",latitude+","+longitude); //Used to check in the logcat the returned values.

        map = new ArcGISMap(Basemap.Type.TOPOGRAPHIC,latitude,longitude, 16);
        mMapView.setMap(map);

        if(graphTemp != null){
            mMapView.getGraphicsOverlays().remove(graphTemp);
        }

        addMarkerToMap(); //Reusing the add marker function.

        return true; //The default value was return false
    }
});
0 Kudos
2 Replies
AlejandroPallares
New Contributor II

Update: From what i've been reading the event.getX and event.getY are screen coordinates, so now i'm going to try to see if i can find a way to properly convert screen coords to lat/long.

0 Kudos
AlexanderNohe1
Regular Contributor II

Hi Alejandro Pallares‌,

I believe what you are looking to do is call the screenToLocation(Point point) method in the mapView.  This would allow you to take the screen coordinates and get them converted to real world coordinates in the mapview and place a point or graphic.  The following documentation should be of help to you:

MapView screenToLocation | arcgis-android 

Thanks,

Alexander