Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

android - Getting coordinates when clicking anywhere on a MapView

I just cant get this working. I tried using the code below with onTouchEventand and it doesn't work. If i return true at the end of the method, i get the toast with the coordinates but can't move a map, and if i return false, i can move a map but cant display a toast after the user clicks on a map. If i get it right, the other onTap method is used only for clicking on a overlays. Has anybody figured this thiing out?

        public boolean onTouchEvent(MotionEvent arg0, MapView arg1) {

       //super.onTouchEvent(arg0);


       int akcija = arg0.getAction(); 

        if(akcija == MotionEvent.ACTION_UP){
            if(!premik) {
                Projection proj = mapView.getProjection();
                GeoPoint loc = proj.fromPixels((int)arg0.getX(), (int)arg0.getY()); 
                String sirina=Double.toString(loc.getLongitudeE6()/1000000);
                String dolzina=Double.toString(loc.getLatitudeE6()/1000000);

                 Toast toast = Toast.makeText(getApplicationContext(), "?irina: "+sirina+" Dolzina: "+dolzina, Toast.LENGTH_LONG);
                toast.show();
            }
        }     

        else if (akcija == MotionEvent.ACTION_DOWN){

            premik= false;

        }
        else if (akcija== MotionEvent.ACTION_MOVE){             
            premik = true;
        }


        return false;
        //return super.onTouchEvent(arg0);
      }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

use dispatchTouchEvent() method. it works. why because the MapActivity inherits the dispatchTouch Event not OnTouchEvent from Activity Class. check this documentation

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int actionType = ev.getAction();
    switch (actionType) {
    case MotionEvent.ACTION_UP:
            Projection proj = mapView.getProjection();
            GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
            String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
            String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);

             Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
            toast.show();

    }

    return super.dispatchTouchEvent(ev);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...