MapView adding pushpins on touch

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

MapView adding pushpins on touch

Postby alinescoo » Sun Sep 12, 2010 5:37 pm

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:

Code: Select all
    public class LocationSelectionActivity extends MapActivity
        GeoPoint p;
        List<Overlay> listOfOverlays;
        MapOverlay mapOverlay;
        private MapView mapView;
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState)
            mapView = (MapView) findViewById(;
            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));
            ZoomControls zoomControls = (ZoomControls) findViewById(;
            zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
            zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
          //---Add a location marker---
            mapOverlay = new MapOverlay();
            listOfOverlays = mapView.getOverlays();

       class MapOverlay extends
            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;
            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());
                            p.getLatitudeE6() / 1E6 + "," +
                            p.getLongitudeE6() /1E6 ,
                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 ?
Junior Developer
Junior Developer
Posts: 24
Joined: Fri Aug 20, 2010 11:08 am


Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests