opensciencemap / vtm

a vector-tile map library written in java - running on android, desktop and within the browser
GNU Lesser General Public License v3.0
238 stars 176 forks source link

Get coordinate from onTouch event #109

Closed Bezzu closed 9 years ago

Bezzu commented 9 years ago

Hello, i would like to know if exist a method to get latitude and longitude from tounch event on map. I'm using this code:

public boolean onTouch(View v, MotionEvent event) {

                    _xTouch = (int) event.getX();
                    _yTouch = (int) event.getY();

                    MapPosition mapPosition = map.getMapPosition();

                    GeoPoint geoPoint = map.getMapPosition().getGeoPoint();
                    double pixelX = Projection.longitudeToPixelX(geoPoint.getLongitude(), mMap.getMapPosition().getZoomLevel());
                    double pixelY = Projection.latitudeToPixelY(geoPoint.getLatitude(), mMap.getMapPosition().getZoomLevel());

                    pixelX -= mapView.getWidth() >> 1;
                    pixelY -= mapView.getHeight() >> 1;

                    // convert the pixel coordinates to a GeoPoint and return it

                    GeoPoint gf= new GeoPoint(Projection.pixelYToLatitude(pixelY + _yTouch, mapPosition.zoomLevel),Projection.pixelXToLongitude(pixelX + _xTouch, mapPosition.zoomLevel));

Thanks.

hjanetzek commented 9 years ago

Hi, one needs to take the current scale, not zoom-level and also rotation and tilt into account: map.viewport().fromScreenPoint(x,y) should give the corresponding GeoPoint. ( should be renamed to getGeoPointForScreenPosition()..)