0

如何在我的代码中添加此功能?网址是这样的:如何在 android 的 osm 地图中添加更多标记,请帮帮我?我想添加功能以打印用户在屏幕上触摸的每个位置的吐司消息,并在吐司消息中的地图上显示其纬度和经度

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;
} 
4

1 回答 1

1

使用这段代码并创建此类的对象并将其添加到 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;
    }        
}

添加这一行

mapView.getOverlays().add(new MapOverlay());
于 2012-12-05T11:53:23.317 回答