开发者

MapView adding pushpins on touch

I managed to get the map shown on the screen. Now the user will move around the map and press on it. After pressing on a point I need to add a push pin on screen on the pressed location. If the user decides to go for another point, when pressing on the chosen point, the first pushpin would disapear and a new one will be drawn on the new location

I did like this:

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.get开发者_如何学CX(),
                    (int) event.getY());
                    Toast.makeText(getBaseContext(), 
                        p.getLatitudeE6() / 1E6 + "," + 
                        p.getLongitudeE6() /1E6 , 
                        Toast.LENGTH_SHORT).show();

            }                            
            return false;
        }        

    } 

This draws the initial pushpin (the map_flag resource). When clicking on the map I get the Toast with the coordonates... all is left to do is on a new click, erase the old pin and add a new one.

But how to do it ?


I would use an ItemizedOverlay, rather than a regular Overlay. Then, all you need to do is update your collection of OverlayItem objects and call populate() on the ItemizedOverlay.

Here is a sample project showing drag-and-drop of a pin using ItemizedOverlay.


This code is working fine .

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

This will work. Make OnTouchEvent's GeoPoint Public...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜