在一个图形页面的任意位置单击时,遇到了坐标 [英] Getting coordinates when clicking anywhere on a MapView

查看:113
本文介绍了在一个图形页面的任意位置单击时,遇到了坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不能得到这个工作。我尝试使用下面onTouchEventand的code和它不工作。 如果我返回true,在方法结束时,我得到的坐标敬酒,但不能移动地图,如果我返回false,我可以移动地图,用户点击地图上后不能显示祝酒。如果我得到它的权利,其他方法的onTap仅用于点击一个覆盖。  有没有人想通了这thiing了呢?

 公共布尔的onTouchEvent(MotionEvent为arg0,图形页面ARG1){

       //super.onTouchEvent(arg0);


       INT akcija = arg0.getAction();

        如果(akcija == MotionEvent.ACTION_UP){
            如果(!premik){
                投影PROJ = MapView.getProjection()在;
                的GeoPoint LOC = proj.fromPixels((int)的arg0.getX(),(int)的arg0.getY());
                字符串Sirina酒店= Double.toString(loc.getLongitudeE6()/ 1000000);
                字符串dolzina = Double.toString(loc.getLatitudeE6()/ 1000000);

                 烤面包烤面包= Toast.makeText(getApplicationContext(),Sirina酒店:+ Sirina酒店+Dolzina:+ dolzina,Toast.LENGTH_LONG);
                toast.show();
            }
        }

        否则,如果(akcija == MotionEvent.ACTION_DOWN){

            premik = FALSE;

        }
        否则,如果(akcija == MotionEvent.ACTION_MOVE){
            premik = TRUE;
        }


        返回false;
        //返回super.onTouchEvent(为arg0);
      }
 

解决方案

使用dispatchTouchEvent()方法。有用。为什么?因为MapActivity继承dispatchTouch事件不会从活动课的onTouchEvent。检查这个文档

  @覆盖
公共布尔dispatchTouchEvent(MotionEvent EV){
    INT操作类型= ev.getAction();
    开关(操作类型){
    案例MotionEvent.ACTION_UP:
            投影PROJ = MapView.getProjection()在;
            的GeoPoint LOC = proj.fromPixels((int)的ev.getX(),(int)的ev.getY());
            字符串经度= Double.toString(((双)loc.getLongitudeE6())/ 1000000);
            字符串纬度= Double.toString(((双)loc.getLatitudeE6())/ 1000000);

             烤面包烤面包= Toast.makeText(getApplicationContext(),经度:+经度+纬度:+纬度,Toast.LENGTH_LONG);
            toast.show();

    }

    返回super.dispatchTouchEvent(EV);
}
 

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

解决方案

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

这篇关于在一个图形页面的任意位置单击时,遇到了坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆