Search code examples
androidopenstreetmapontouchlistener

How to make the touch event in openstreetmap android


I am suffering in OSM map. This new for me. I want to print the Toast message on Single-click and on Long press I try all things that can i do. But i don't get the right way. Here is my code. Please give me right way to solve this. Thanks in dvance. please

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map1);
    final MapView mapView = (MapView) findViewById(R.id.mapViewosm);
    mapView.setBuiltInZoomControls(true);

    MapController myMapController = mapView.getController();
    myMapController.setZoom(13);

    ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
    mapView.getOverlays().add(myScaleBarOverlay);

    Drawable marker = getResources().getDrawable(
            android.R.drawable.star_big_on);
    int markerWidth = marker.getIntrinsicWidth();
    int markerHeight = marker.getIntrinsicHeight();
    marker.setBounds(0, markerHeight, markerWidth, 0);

    ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
            getApplicationContext());

    myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
    mapView.getOverlays().add(myItemizedOverlay);

    GeoPoint myPoint1 = new GeoPoint(0 * 1000000, 0 * 1000000);
    myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
    GeoPoint myPoint2 = new GeoPoint(50 * 1000000, 50 * 1000000);
    myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");

    myLocationOverlay = new MyLocationOverlay(this, mapView);
    mapView.getOverlays().add(myLocationOverlay);
    myLocationOverlay.enableMyLocation();

    myLocationOverlay.runOnFirstFix(new Runnable() {
        public void run() {
            mapView.getController().animateTo(
                    myLocationOverlay.getMyLocation());
        }
    });
    // 2nd
    // // --- Create Overlay
    // overlayItemArray = new ArrayList<OverlayItem>();
    //
    // DefaultResourceProxyImpl defaultResourceProxyImpl = new
    // DefaultResourceProxyImpl(
    // this);
    // MyItemizedIconOverlay myItemizedIconOverlay = new
    // MyItemizedIconOverlay(
    // overlayItemArray, null, defaultResourceProxyImpl);
    // mapView.getOverlays().add(myItemizedIconOverlay);
    // // ---
    // locationManager = (LocationManager)
    // getSystemService(Context.LOCATION_SERVICE);
    //
    // // for demo, getLastKnownLocation from GPS only, not from NETWORK
    // Location lastLocation = locationManager
    // .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    // if (lastLocation != null) {
    // updateLoc(lastLocation);
    // }

    // 1st
     anotherOverlayItemArray = new ArrayList<OverlayItem>();
     anotherOverlayItemArray.add(new OverlayItem("0, 0", "0, 0",
     new GeoPoint(0, 0)));
     anotherOverlayItemArray.add(new OverlayItem("US", "US", new GeoPoint(
     38.883333, -77.016667)));
     ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new
     ItemizedIconOverlay<OverlayItem>(
     this, anotherOverlayItemArray, myOnItemGestureListener);
     mapView.getOverlays().add(anotherItemizedIconOverlay);

}

// 1st
 OnItemGestureListener<OverlayItem> myOnItemGestureListener = new
 OnItemGestureListener<OverlayItem>() {

 @Override
 public boolean onItemLongPress(int arg0, OverlayItem arg1) {
 // TODO Auto-generated method stub

 Toast.makeText(getApplicationContext(), "long tap",
 Toast.LENGTH_SHORT).show();
 return true;
 }

 @Override
 public boolean onItemSingleTapUp(int index, OverlayItem item) {

 Toast.makeText(getApplicationContext(), "single tap",
 Toast.LENGTH_SHORT).show();
 Toast.makeText(
 Map.this,
 item.mDescription + "\n" + item.mTitle + "\n"
 + item.mGeoPoint.getLatitudeE6() + " : "
 + item.mGeoPoint.getLongitudeE6(),
 Toast.LENGTH_LONG).show();
 return true;
 }

 };

Solution

  • Finally i got solution for this problem. here is my answer

    @Override
        public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {
    
            Projection proj = mapView.getProjection();
            p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
             proj = mapView.getProjection();
             loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.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_SHORT);
             toast.show();
            return true;
        }
    
    private void addLocation(double lat, double lng) {
        // ---Add a location marker---
    
        p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
    
        Drawable marker = getResources().getDrawable(
                android.R.drawable.star_big_on);
    
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
    
        marker.setBounds(0, markerHeight, markerWidth, 0);
    
        ResourceProxy resourceProxy = new DefaultResourceProxyImpl(
                getApplicationContext());
    
        myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
    
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(myItemizedOverlay);
    
        mapView.invalidate();
    }