开发者

Getting coordinates when clicking anywhere on a MapView

开发者 https://www.devze.com 2022-12-29 05:55 出处:网络
I just cant get this working. I tried using the code below with onTouchEventand andit doesn\'t work.

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开发者_StackOverflow社区: "+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);
      }


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号