Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get coordinates by clicking on map (openstreetmaps)

How can we get the coordinates of a point by clicking on it in open street maps?

tried:

public void onClick(View v) {
    Projection proj = mapView.getProjection();
    IGeoPoint p = proj.fromPixels(v.getX(), v.getY());
    System.out.println("x: "+ v.getX() + " y: "+ v.getY());
}

cheers, Thanasio

like image 280
thanasio2 Avatar asked May 21 '13 08:05

thanasio2


People also ask

How do I extract coordinates from Openstreetmap?

On the main slippy map you can right-click then click 'Show address'. The coordinates for that location will come up on the left side of the map.

Can you enter coordinates in Google Maps app?

Enter Coordinates on AndroidOpen Google Maps, enter the coordinates into the Search box at the top, and hit the Search key on the keyboard. You'll see a pin appear on the map for the spot. Swipe up from the bottom to get directions or view details for the location. Get Google Maps for Android, free on Google Play.


2 Answers

Use dispatchTouchEvent() method. It works because the MapActivity inherits the dispatchTouch event, not the OnTouchEvent from Activity Class.

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    int actionType = ev.getAction();
    switch (actionType) {
    case MotionEvent.ACTION_UP:
            Projection proj = mapView.getProjection();
            GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.getY()); 
            String longitude = Double.toString(((double)loc.getLongitudeE6())/1000000);
            String latitude = Double.toString(((double)loc.getLatitudeE6())/1000000);

             Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "+ longitude +" Latitude: "+ latitude , Toast.LENGTH_LONG);
            toast.show();

    }
return super.dispatchTouchEvent(ev);
}
like image 159
Sandeep Avatar answered Sep 20 '22 14:09

Sandeep


This is my own implementation of MapView to get the location clicking the map.

public class MapViewLoc extends MapView {

    private Overlay tapOverlay;
    private OnTapListener onTapListener;

    protected MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase tileProvider, Handler tileRequestCompleteHandler, AttributeSet attrs) {
        super(context, tileSizePixels, resourceProxy, tileProvider, tileRequestCompleteHandler, attrs);
    }

    public MapViewLoc(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MapViewLoc(Context context, int tileSizePixels) {
        super(context, tileSizePixels);
    }

    public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy) {
        super(context, tileSizePixels, resourceProxy);
    }

    public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider) {
        super(context, tileSizePixels, resourceProxy, aTileProvider);
    }

    public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider, Handler tileRequestCompleteHandler) {
        super(context, tileSizePixels, resourceProxy, aTileProvider, tileRequestCompleteHandler);
    }

    private void prepareTagOverlay(){

       this.tapOverlay = new Overlay(this.getContext()) {

            @Override
            protected void draw(Canvas c, MapView osmv, boolean shadow) {

            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {

                Projection proj = mapView.getProjection();
                GeoPoint p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
                proj = mapView.getProjection();

                final GeoPoint geoPoint = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());

                if(MapViewLoc.this.onTapListener != null){

                    MapViewLoc.this.onTapListener.onMapTapped(geoPoint);

                    Location location = new Location("");
                    location.setLatitude((double) geoPoint.getLatitudeE6() / 1000000);
                    location.setLongitude((double) geoPoint.getLongitudeE6() / 1000000);
                    location.setAccuracy(Criteria.ACCURACY_FINE);

                    MapViewLoc.this.onTapListener.onMapTapped(location);
                }

                return true;
            }
        };
    }

    public void addTapListener(OnTapListener onTapListener){

        this.prepareTagOverlay();

        this.getOverlays().add(0, this.tapOverlay);

        this.onTapListener = onTapListener;
    }

    public void removeTapListener(){

        if(this.tapOverlay != null && this.getOverlays().size() > 0){

            this.getOverlays().remove(0);
        }

        this.tapOverlay = null;
        this.onTapListener = null;
    }

    public interface OnTapListener{

        void onMapTapped(GeoPoint geoPoint);

        void onMapTapped(Location location);

    }

}

To get location, only set the interface OnTapListener.

mapView.addTapListener(new MapViewLoc.OnTapListener() {

            @Override
            public void onMapTapped(GeoPoint geoPoint) {}

            @Override
            public void onMapTapped(Location location) {

                Toast toast = Toast.makeText(getApplicationContext(),
                        "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(),
                        Toast.LENGTH_SHORT);

                toast.show();
            }
        });
like image 24
Dani Avatar answered Sep 19 '22 14:09

Dani