MapView在触摸时添加图钉

时间:2010-09-12 16:36:14

标签: android android-mapview pushpin

我设法让屏幕上显示地图。现在,用户将在地图上移动并按下它。按下一个点后,我需要在按下位置的屏幕上添加一个推针。如果用户决定转到另一个点,当按下所选点时,第一个图钉将消失,并且将在新位置绘制新图钉

我确实喜欢这个:

public class LocationSelectionActivity extends MapActivity 
{    
     GeoPoint p;
     List<Overlay> listOfOverlays;
     MapOverlay mapOverlay;

    private MapView mapView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locationselection);

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setSatellite(false);

        final MapController mc = mapView.getController();
        String coordinates[] = {"46.540606", "22.454542"};

        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

         p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(10); 


        ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomcontrols);
        zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomIn();
                }
        });
        zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        mc.zoomOut();
                }
        });

      //---Add a location marker---
        mapOverlay = new MapOverlay();
        listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();
    }




   class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.map_flag);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-48, null);

            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---
            if (event.getAction() == 1) {                
                GeoPoint 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;
        }        

    } 

这将绘制初始图钉(map_flag资源)。当点击地图时,我得到了带有coordonates的Toast ......剩下要做的就是点击一下,删除旧的pin并添加一个新的。

但怎么办呢?

3 个答案:

答案 0 :(得分:15)

我会使用ItemizedOverlay,而不是常规Overlay。然后,您需要做的就是更新OverlayItem个对象的集合,并在populate()上致电ItemizedOverlay

以下是a sample project使用ItemizedOverlay显示引脚的拖放。

答案 1 :(得分:3)

此代码工作正常。

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

答案 2 :(得分:1)

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

这会奏效。使OnTouchEvent的GeoPoint Public ...