不会触发地图上的OnTap()事件

时间:2011-06-13 07:21:58

标签: android map

您好我无法触发ontap()事件

我想在我点击地图时添加标记,当我点击另一个地点时,第一个标记应该消失,标记应该添加到新位置...

直到现在我已经到了这一点。可以告诉我哪里出错! 提前谢谢

源代码

public class GetLocation extends MapActivity  implements OnClickListener {

MapView mapView;

    MapController mc;

    GeoPoint p;
    int range;
    String category;
    Button view, traffic;
    private static final String Tag = "GetLocation class";
    ZoomControls zoomControls;
    Canvas canvas;
    MapOverlay itemizedoverlay;
    List<Overlay> mapOverlays;

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("Tag","Inside onCreate");
            setContentView(R.layout.main);
            mapView = (MapView) findViewById(R.id.myMapView1);
            LinearLayout zoom = (LinearLayout)findViewById(R.id.zoom); 
            mapView.setReticleDrawMode(
                    MapView.ReticleDrawMode.DRAW_RETICLE_UNDER);
         //   Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
            view=(Button)findViewById(R.id.BtnView);
            traffic=(Button)findViewById(R.id.BtnTraffic);
            Bundle extra=getIntent().getExtras();
            if(extra != null)
            {
                category=extra.getString("category");
                range=extra.getInt("range");
            }
            view.setOnClickListener(this);
            traffic.setOnClickListener(this);
            zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
            zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mc.zoomIn();
                    }
            });
            zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mc.zoomOut();
                    }
            });
            mapOverlays = mapView.getOverlays();
            Drawable drawable = this.getResources().getDrawable(R.drawable.mark);
            itemizedoverlay = new MapOverlay(drawable,this);
            mc = mapView.getController();
            mapView.invalidate();
            mc.setZoom(17); 
            mapView.invalidate();
        //    mapView.setSatellite(true);
            mapView.setStreetView(true);
         //   mapView.setOnClickListener(this);
            Log.d("Tag","Exit onCreate");
        }
    class MapOverlay extends com.google.android.maps.ItemizedOverlay<OverlayItem>
    {

        private ArrayList<OverlayItem> mOverlay = new ArrayList<OverlayItem>();
        private Context mContext;
        private boolean  isPinch = false;
        public MapOverlay(Drawable defaultMarker,Context context) {
            super(boundCenterBottom((defaultMarker)));
            mContext = context;
            // TODO Auto-generated constructor stub
        }


        public void addOverlayItem(OverlayItem overlayItem)
        {

            if(!mOverlay.contains(overlayItem)){

                mOverlay.add(overlayItem);
            }

           populate();

        }


        @Override
        protected OverlayItem createItem(int i) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public int size() {
            // TODO Auto-generated method stub
            return mOverlay.size();
        }
        public boolean onTap(GeoPoint p, MapView map)
        {
            if ( isPinch )
            {
                Log.i("onTap","in if!");
                return false;
            }
            else
            {
                Log.i("onTap","TAP!");
                if ( p!=null )
                {
                     OverlayItem overlayitem = new OverlayItem(p," ", " ");

                    itemizedoverlay.addOverlayItem(overlayitem);
                     mapOverlays.add(itemizedoverlay);
                     Toast.makeText(getBaseContext(), 
                             p.getLatitudeE6() / 1E6 + ",on Tap" + 
                             p.getLongitudeE6() /1E6 , 
                             Toast.LENGTH_SHORT).show();
                     Log.d("Tag","Exit TAp");
                    return true;            // We handled the tap
                }
                else
                {
                    return false;           // Null GeoPoint
                }
            }
        }
}

3 个答案:

答案 0 :(得分:0)

这是我的代码 希望你能得到一些想法

Point p1=new Point(0,0);
mapView.getProjection().toPixels(mapPoint, p1);// mapPoint is GeoPoint object
inDrag=item;  // item get from List object and inDrag is an OverlayItem object
items.remove(inDrag); items is list object
populate();
GeoPoint pt=mapView.getProjection().fromPixels(p1.x+xDragImageOffset,p1.y);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),inDrag.getSnippet());
items.add(toDrop);
populate();

答案 1 :(得分:0)

查看这篇文章:http://mobiforge.com/developing/story/using-google-maps-android 它有一个叫做“添加标记”的部分

此外,您还有另一个更高级的示例: https://github.com/commonsguy/cw-advandroid/blob/master/Maps/NooYawkTouch/src/com/commonsware/android/maps/NooYawk.java

希望它对你有所帮助。

答案 2 :(得分:0)

当用户点击标记

时,在itemizedoverlay中点击的功能基本上被触发

你将创建一个扩展覆盖层来检测水龙头。 检查此overlays example