0

My map code is this:

public class ExampleActivity extends MapActivity {
    private MapView map = null;
    private MyLocationOverlay me = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map = (MapView) findViewById(R.id.map);

        map.getController().setCenter(getPoint(18.5236296, 73.8478429));
        map.getController().setZoom(17);
        map.setBuiltInZoomControls(true);

        Drawable marker = getResources().getDrawable(R.drawable.marker);

        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                marker.getIntrinsicHeight());

        map.getOverlays().add(new SitesOverlay(marker));

        me = new MyLocationOverlay(this, map);
        map.getOverlays().add(me);
    }

    @Override
    public void onResume() {
        super.onResume();

        me.enableCompass();
    }

    @Override
    public void onPause() {
        super.onPause();

        me.disableCompass();
    }

    @Override
    protected boolean isRouteDisplayed() {

        return (false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_S) {

            map.setSatellite(!map.isSatellite());
            return (true);
        } else if (keyCode == KeyEvent.KEYCODE_Z) {

            map.displayZoomControls(true);
            return (true);
        }

        return (super.onKeyDown(keyCode, event));
    }

    private GeoPoint getPoint(double lat, double lon) {

        return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
    }

    private class SitesOverlay extends ItemizedOverlay<OverlayItem> {

        private List<OverlayItem> items = new ArrayList<OverlayItem>();
        private Drawable marker = null;
        private OverlayItem inDrag = null;
        private ImageView dragImage = null;
        private int xDragImageOffset = 0;
        private int yDragImageOffset = 0;
        private int xDragTouchOffset = 0;
        private int yDragTouchOffset = 0;

        public SitesOverlay(Drawable marker) {
            super(marker);
            this.marker = marker;

            dragImage = (ImageView) findViewById(R.id.drag);
            xDragImageOffset = dragImage.getDrawable().getIntrinsicWidth() / 2;
            yDragImageOffset = dragImage.getDrawable().getIntrinsicHeight();

            items.add(new OverlayItem(getPoint(18.5236296, 73.8478429), "Pune",
                    "Wakad"));

            populate();
        }

        @Override
        protected OverlayItem createItem(int i) {

            return (items.get(i));
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);

            boundCenterBottom(marker);
        }

        @Override
        public int size() {

            return (items.size());
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) {

            final int action = event.getAction();
            final int x = (int) event.getX();
            final int y = (int) event.getY();
            boolean result = false;

            if (action == MotionEvent.ACTION_DOWN) {

                for (OverlayItem item : items) {
                    Point p = new Point(0, 0);

                    map.getProjection().toPixels(item.getPoint(), p);

                    if (hitTest(item, marker, x - p.x, y - p.y)) {

                        result = true;
                        inDrag = item;
                        items.remove(inDrag);
                        populate();

                        xDragTouchOffset = 0;
                        yDragTouchOffset = 0;

                        setDragImagePosition(p.x, p.y);
                        dragImage.setVisibility(View.VISIBLE);

                        xDragTouchOffset = x - p.x;
                        yDragTouchOffset = y - p.y;

                        break;
                    }
                }
            } else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {

                setDragImagePosition(x, y);
                result = true;
            } else if (action == MotionEvent.ACTION_UP && inDrag != null) {

                dragImage.setVisibility(View.GONE);

                GeoPoint pt = map.getProjection().fromPixels(
                        x - xDragTouchOffset, y - yDragTouchOffset);
                OverlayItem toDrop = new OverlayItem(pt, inDrag.getTitle(),
                        inDrag.getSnippet());

                items.add(toDrop);
                populate();

                inDrag = null;
                result = true;
            }

            return (result || super.onTouchEvent(event, mapView));
        }

        private void setDragImagePosition(int x, int y) {

            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) dragImage
                    .getLayoutParams();

            lp.setMargins(x - xDragImageOffset - xDragTouchOffset, y
                    - yDragImageOffset - yDragTouchOffset, 0, 0);
            dragImage.setLayoutParams(lp);
        }
    }
}

this shows a marker on the map, and i can drag and drop this marker to some other place on the map....so now, my question is how to get the latitude and longitude of the current position of the marker??

Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • http://stackoverflow.com/questions/4238530/android-mapview-draggable-marker – MKJParekh Jun 28 '12 at 09:52
  • that's the code i used....to get draggable marker...but there is nothing explained about how to get the latitude and longitude of the current marker location – Archie.bpgc Jun 28 '12 at 09:57
  • Because he was not paid to do that, He did that and put that for community to use, If you study the code there is the thing is done, As I have used it to drag the marker, and soon as the user drop it i did shown a dialog with the current place lat long..so i know its possible – MKJParekh Jun 28 '12 at 10:03

1 Answers1

2

In the onTouchListener of Drag and Drop Code, you can use the below code to get The GeoPoint

if (action == MotionEvent.ACTION_UP && inDrag != null) {
    GeoPoint pt = map.getProjection().fromPixels(
                        x - xDragTouchOffset, y - yDragTouchOffset);

    taplat = pt.getLatitudeE6() / 1E6; // latitude
    taplon = pt.getLongitudeE6() / 1E6; // longitude
}
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • @ MKJPareksh is there a way to draw an area on the map, by dragging our finger, and then later get the area details like - if its a circle, 1.zoomlevel 2.center point lat and long 3. radius ??? – Archie.bpgc Jun 28 '12 at 10:29
  • @Archie.bpgc Nope, I didnt have done anything like that, But I suppose the Same OnTouchListner will be the Starting point. – MKJParekh Jun 28 '12 at 10:35