4

我在 OSM 地图中受苦。这对我来说是新的。我想在单击和长按时打印 Toast 消息,我尝试了所有我能做的事情。但我没有得到正确的方法。这是我的代码。请给我正确的方法来解决这个问题。提前致谢。请

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

 };
4

2 回答 2

4

在 osm 中,您可以使用MapEventsReceiver like blew 来检测水龙头:

MapEventsReceiver mReceive = new MapEventsReceiver() {
        @Override
        public boolean singleTapConfirmedHelper(GeoPoint p) {

            // write your code here

            return false;
        }

        @Override
        public boolean longPressHelper(GeoPoint p) {
            // write your code here
            return false;
        }
    };
    MapEventsOverlay OverlayEvents = new MapEventsOverlay(getBaseContext(), mReceive);
    map.getOverlays().add(OverlayEvents);

来源:https ://github.com/osmdroid/osmdroid/issues/295

于 2017-12-24T20:54:48.863 回答
2

最后我得到了这个问题的解决方案。这是我的答案

@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();
}
于 2013-10-19T08:12:56.777 回答