Android 上的谷歌地图无法放大和缩小

发布于 2024-12-14 06:29:37 字数 1609 浏览 3 评论 0原文

当我设置触摸侦听器以获取触摸位置经纬度时,当我从代码中删除触摸侦听器时,放大和缩小不起作用。那么如何实现谷歌地图的放大和缩小以及长时间触摸位置呢?

我的代码如下

   mapView = (MapView) findViewById(R.id.map);
    mapView.setBuiltInZoomControls(true);
     mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override

            public boolean onTouch(View v, MotionEvent event) {
                Log.i("!!!!!","*****");

            //---when user lifts his finger---

                if (event.getAction() == MotionEvent.ACTION_UP) {

                     Projection p2 = mapView.getProjection();
                     GeoPoint geoPoint = p2.fromPixels((int) event.getX(), (int)event.getY());
                     int latitude = geoPoint.getLatitudeE6();
                     int longitude = geoPoint.getLongitudeE6();
                     String aString = Integer.toString(latitude);
                     String aString2 = Integer.toString(longitude);

                     Log.i("latitude,longitude",aString+",,"+aString2);
                     if((latitude < -34.538238) && (latitude > -34.672182))
                     {
                      if((longitude < -58.347702) && (longitude > -58.528976))
                      {
                         Log.i("lat,long",aString+",,"+aString2);
                      }
                      else 
                      {
                             Log.i("out of region",aString+",,"+aString2);

                      }}
                    Log.i("@@@@@!!!!!","*****");



                }
                return true;

            }


            });

When I set on touch listener for getting touch location lat long then me zoom in and out is not working its working when I remove on touch listener from my code. So how to achieve to work zoom in and out in Google map and get touch location lat long?

My code is below

   mapView = (MapView) findViewById(R.id.map);
    mapView.setBuiltInZoomControls(true);
     mapView.setOnTouchListener(new View.OnTouchListener() {
            @Override

            public boolean onTouch(View v, MotionEvent event) {
                Log.i("!!!!!","*****");

            //---when user lifts his finger---

                if (event.getAction() == MotionEvent.ACTION_UP) {

                     Projection p2 = mapView.getProjection();
                     GeoPoint geoPoint = p2.fromPixels((int) event.getX(), (int)event.getY());
                     int latitude = geoPoint.getLatitudeE6();
                     int longitude = geoPoint.getLongitudeE6();
                     String aString = Integer.toString(latitude);
                     String aString2 = Integer.toString(longitude);

                     Log.i("latitude,longitude",aString+",,"+aString2);
                     if((latitude < -34.538238) && (latitude > -34.672182))
                     {
                      if((longitude < -58.347702) && (longitude > -58.528976))
                      {
                         Log.i("lat,long",aString+",,"+aString2);
                      }
                      else 
                      {
                             Log.i("out of region",aString+",,"+aString2);

                      }}
                    Log.i("@@@@@!!!!!","*****");



                }
                return true;

            }


            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-12-21 06:29:37

当您setOnTouchListener时,您的setBuiltInZoomControls(true)将不起作用。
您可以通过命令displayZoomControls(true)显示缩放按钮。您的地图不会缩放,也不会通过触摸或多点触摸移动,但您可以通过单击缩放按钮来缩放地图。

另一种方法是当不需要处理触摸但需要缩放地图时,通过 mapView.setOnTouchListener(null) 关闭监听器,并通过 setBuiltInZoomControls(true) 缩放地图。当您需要处理触摸时,您可以设置侦听器 mapView.setOnTouchListener(new OnTouchLister(...)) 但通过触摸进行缩放的机会就会丢失。当不需要触摸时,执行 mapView.setOnTouchListener(null) 并再次执行 setBuiltInZoomControls(true) 来缩放地图。

When you setOnTouchListener your setBuiltInZoomControls(true) will not work.
You may show zoom button by command displayZoomControls(true). Your map will not zoom and will not move by touch or multitouch but you will can zooming map by clicking to the zoom button.

The other way is to shut off listener by mapView.setOnTouchListener(null) and zooming map by setBuiltInZoomControls(true) when you don't need processing touch but need to zoom map. In the moment when you need to process touch you set listener mapView.setOnTouchListener(new OnTouchLister(...)) but opportunity zooming by touch is lost. When touch isn't needed do mapView.setOnTouchListener(null) and again setBuiltInZoomControls(true) to zoom map.

茶色山野 2024-12-21 06:29:37

试试这个。应该能够获得缩放控件和您之前所做的 GeoPoint 内容。

public boolean onTouchEvent(MotionEvent event,MapView m) {
    boolean status = false;
    int start,stop = 0;
    long long_click_time = 1500;  //1.5 seconds 
    if(e.getAction() == MotionEvent.ACTION_DOWN){
       start = event.getEventTime();
    }

    if(e.getAction()==MotionEvent.ACTION_UP){
       stop = event.getEventTime();
    }

    if(stop-start>long_click_time){
       //do your work
       status = true;
    }

    return status;
}

返回 false 应该会给你缩放控件。该方法应该位于 Overlay 的子类中。在下面的代码中,“Touchy”类继承自 Overlay。

List<Overlay> overlayList = this.map.getOverlays();
Touchy touchy = new Touchy(Maps.this);
overlayList.add(touchy);

Try this. Should be able to get the zoom controls and the GeoPoint stuff you were doing earlier.

public boolean onTouchEvent(MotionEvent event,MapView m) {
    boolean status = false;
    int start,stop = 0;
    long long_click_time = 1500;  //1.5 seconds 
    if(e.getAction() == MotionEvent.ACTION_DOWN){
       start = event.getEventTime();
    }

    if(e.getAction()==MotionEvent.ACTION_UP){
       stop = event.getEventTime();
    }

    if(stop-start>long_click_time){
       //do your work
       status = true;
    }

    return status;
}

Returning false should give you the zoom controls. The method should be in a subclass of Overlay. In the code below, the class "Touchy" inherits from Overlay.

List<Overlay> overlayList = this.map.getOverlays();
Touchy touchy = new Touchy(Maps.this);
overlayList.add(touchy);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文