user631063
user631063

Reputation:

Returning a GeoPoint on user touch MapView Android

I'm having trouble trying to figure out a way to return a GeoPoint with the longitude and latitude of the location the user just touched. I have seen and read tons or examples that display the longitude and latitude in a toast by overriding dispatchTouchEvent(). I believe this won't work for my case for the following reason.

I want the user to be able click a button that will then call another method that will return a geopoint on the user's next tap. So, in short, a geopoint is only returned on the tap after the user clicks the button.

The only work around I can see using the dispatchTouchEvent() is the following.

If the button has been clicked, write to the shared preferences. Then in dispatchTouchEvent() check to see if the button has been clicked. If it has not been clicked,do nothing. If it has been clicked write the longitude and latitude to shared preferences and then read them from shared preferences and create a GeoPoint out of them in a different method.

I don't feel like that is a very elegant idea. I feel like there should be a much cleaner way of doing this(like somehow returning just a geopoint)

I hope this makes a little bit of sense. I'll try to answer any questions I can.

Thanks in advance.

Upvotes: 2

Views: 1786

Answers (1)

Awais Tariq
Awais Tariq

Reputation: 7754

I just read the title of your question..so pardon me if put the wrong answer..:D

    mapView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            GeoPoint p = null;

            if (event.getAction() == MotionEvent.ACTION_UP) {
                p = mapView.getProjection().fromPixels((int) event.getX(),
                        (int) event.getY());
                mapBackButton.setText(p.getLatitudeE6() / 1E6 + ","
                        + p.getLongitudeE6() / 1E6 + "Action is : "
                        + event.getAction());
                return true;
                Toast.makeText(
                        getBaseContext(),
                        p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6()
                                / 1E6 + "Action is : " + event.getAction(),
                        Toast.LENGTH_SHORT).show();
            }
            return false;
        }
    });

Upvotes: 3

Related Questions