How to add touch event to show latitude and longitude in a toast in OSM

☆樱花仙子☆ 提交于 2020-01-07 02:31:09

问题


How do I add this function below in my code? The url is this: how to add more marker in osm map in android please help me? I want to add feature to print toast message of every location where user touches on the screen and displays its latitude and longitude on map in toast message

public boolean onTouchEvent(MotionEvent event, MapView mapView) {   

    if (event.getAction() == 1) {                
        GeoPoint geopoint = (GeoPoint) mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
        // latitude
        double lat = geopoint.getLatitudeE6() / 1E6;
        // longitude
        double lon = geopoint.getLongitudeE6() / 1E6;
        Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
    }                            
    return false;
} 

回答1:


Use this piece of code and create an object of this class and add it to the mapsOverlay

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
       //...
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---
        if (event.getAction() == MotionEvent.ACTION_UP) {                
            GeoPoint 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;
    }        
}

add this line

mapView.getOverlays().add(new MapOverlay());


来源:https://stackoverflow.com/questions/13721604/how-to-add-touch-event-to-show-latitude-and-longitude-in-a-toast-in-osm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!