Android:OnTouchListener在MapView上没有响应

时间:2012-07-21 19:21:30

标签: android 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;
}

我想要做的是在触摸屏幕时抓住,但从不调用该方法。我已经搜索过并发现了这个:   OnTouch in map

我尝试了一切,唯一有效的代码是:

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

问题在于,如果我使用此代码,我的MapView会丢失它已经拥有的自然投影和缩放,那么我如何在MapView中拥有所有内容?

谢谢。

2 个答案:

答案 0 :(得分:1)

有两种方法可以实现您的目标,下面是代码:

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

你可以这样做,但这不会在地图上添加任何叠加项目/图钉/标记。

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

这是指向触摸地图上任何位置的教程的链接

Detect touch on MapView

使标记/图钉/项目覆盖在地图上,并在点击每个图钉/标记时进行检测

Detect touch on marker in MapView

我希望这会对你有所帮助:)。

感谢。

答案 1 :(得分:0)

如果您只想看到触摸屏幕的时间,您仍然可以返回false,这将允许所有其他拖动和缩放触摸事件生效。

如果你想要一个单独的触摸事件不继续其他事件,你只会返回true。