简体   繁体   English

Android:OnTouchListener在MapView上没有响应

[英]Android: OnTouchListener not responding at MapView

I have the following code to show my actual location on Google Maps: 我有以下代码来显示我在Google地图上的实际位置:

public class LocationProjectActivity extends MapActivity implements OnTouchListener {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
private GeoPoint MyPoint;
public static int longitude;
public static int latitude;
private GeoPoint destinationPoint;

public void onCreate(Bundle bundle) {
  super.onCreate(bundle);
  setContentView(R.layout.main);
  mapView = (MapView) findViewById(R.id.mapview);
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(true);
  mapView.computeScroll();
  mapController = mapView.getController();
  mapController.setZoom(13);
  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new GeoUpdateHandler());
  myLocationOverlay = new MyLocationOverlay(this, mapView);
  mapView.getOverlays().add(myLocationOverlay);
  myLocationOverlay.runOnFirstFix(new Runnable() {
    public void run() {
      mapView.getController().animateTo(myLocationOverlay.getMyLocation());
    }
  });
}

@Override
public boolean onTouch(View v, MotionEvent event) {
  System.out.println("SCREEN WAS CLICKED");
  return true;
}

What I want to do is catch when I touch the screen, but the method is never called. 我想要做的是当我触摸屏幕时捕获,但从未调用该方法。 I already searched and found this: OnTouch in map 我已经搜索过并发现了这个: OnTouch在地图上

I tried everything and the only code that works was this: 我尝试了一切,唯一有效的代码是:

mapView.setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
    // Your code and remember to return true!        
    return (true);
  }
});

The problem is that if I use this code, my MapView loses the natural fling and zoom that it already has, so how can I have everything in my MapView? 问题是如果我使用这个代码,我的MapView会失去它已经拥有的自然投射和缩放,那么我如何在MapView中拥有所有内容呢?

Thank you. 谢谢。

There are two ways you can achieve what you are looking for and below is the code: 有两种方法可以实现您的目标,下面是代码:

@Override 
      public boolean onTap(GeoPoint p, MapView mapView) {
            Log.d("tap event ", "tap called");
            mapOverlays = mapView.getOverlays();
            drawable  =getResources().getDrawable(R.drawable.marker);
            itemizedOverlay = new SitesOverlay(drawable);
            int lat=(int)p.getLatitudeE6();
            int lng=(int)p.getLongitudeE6();


            GeoPoint point = new GeoPoint(lat,lng);
            Log.d("tap event ", "tapcalled"+lat+""+lng);
            OverlayItem overlayitem = new OverlayItem(point, "", "");

            items.add(overlayitem);
            populate();
            Log.d("tap event ", "populated");
            //      mapOverlays.add(itemizedOverlay);   


            return true;
        }

and you can do this in this way but this doesn't add any overlay item/pin/marker on the map. 你可以这样做,但这不会在地图上添加任何叠加项/图钉/标记。

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{   
    //---when user lifts his finger---
    if (event.getAction() == 1) {                
        p = mapView.getProjection().fromPixels(
            (int) event.getX(),
            (int) event.getY());
            Toast.makeText(getBaseContext(), 
                p.getLatitudeE6() / 1E6 + "," + 
                p.getLongitudeE6() /1E6 , 
                Toast.LENGTH_SHORT).show();
    }                            
    return false;
}    

Here is the link to the tutorial to detect touch any where on the Map 这是指向触摸地图上任何位置的教程的链接

Detect touch on MapView 检测MapView上的触摸

To make marker/pin/item overlay over the map and detect on click of each pin/marker 使标记/图钉/项目覆盖在地图上并检测每个图钉/标记的点击

Detect touch on marker in MapView 在MapView中检测标记上的触摸

I hope this will help you :) 我希望这能帮到您 :)

Thanks. 谢谢。

If you just want to see when you touch the screen, you can still return false which will allow all the other drag and zoom touch events to work. 如果您只想看到触摸屏幕的时间,您仍然可以返回false,这将允许所有其他拖动和缩放触摸事件工作。

You would only return true if you want a separate touch event to NOT continue to the others. 如果您希望单独的触摸事件不继续其他触摸事件,则只会返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM