【问题标题】:get coordinates by clicking on map (openstreetmaps)通过点击地图获取坐标(openstreetmaps)
【发布时间】:2013-05-15 22:48:00
【问题描述】:

我们如何在打开的街道地图中点击某个点的坐标?

试过了:

public void onClick(View v) {
    Projection proj = mapView.getProjection();
    IGeoPoint p = proj.fromPixels(v.getX(), v.getY());
    System.out.println("x: "+ v.getX() + " y: "+ v.getY());
}

干杯, 塔纳西奥

【问题讨论】:

    标签: android maps onclicklistener osmdroid


    【解决方案1】:

    使用dispatchTouchEvent() 方法。它之所以有效,是因为 MapActivity 继承了 dispatchTouch 事件,而不是来自 Activity 类的 OnTouchEvent

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int actionType = ev.getAction();
        switch (actionType) {
        case MotionEvent.ACTION_UP:
                Projection proj = mapView.getProjection();
                GeoPoint loc = proj.fromPixels((int)ev.getX(), (int)ev.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_LONG);
                toast.show();
    
        }
    return super.dispatchTouchEvent(ev);
    }
    

    【讨论】:

      【解决方案2】:

      这是我自己的 MapView 实现,用于获取单击地图的位置。

      public class MapViewLoc extends MapView {
      
          private Overlay tapOverlay;
          private OnTapListener onTapListener;
      
          protected MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase tileProvider, Handler tileRequestCompleteHandler, AttributeSet attrs) {
              super(context, tileSizePixels, resourceProxy, tileProvider, tileRequestCompleteHandler, attrs);
          }
      
          public MapViewLoc(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public MapViewLoc(Context context, int tileSizePixels) {
              super(context, tileSizePixels);
          }
      
          public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy) {
              super(context, tileSizePixels, resourceProxy);
          }
      
          public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider) {
              super(context, tileSizePixels, resourceProxy, aTileProvider);
          }
      
          public MapViewLoc(Context context, int tileSizePixels, ResourceProxy resourceProxy, MapTileProviderBase aTileProvider, Handler tileRequestCompleteHandler) {
              super(context, tileSizePixels, resourceProxy, aTileProvider, tileRequestCompleteHandler);
          }
      
          private void prepareTagOverlay(){
      
             this.tapOverlay = new Overlay(this.getContext()) {
      
                  @Override
                  protected void draw(Canvas c, MapView osmv, boolean shadow) {
      
                  }
      
                  @Override
                  public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView) {
      
                      Projection proj = mapView.getProjection();
                      GeoPoint p = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
                      proj = mapView.getProjection();
      
                      final GeoPoint geoPoint = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
      
                      if(MapViewLoc.this.onTapListener != null){
      
                          MapViewLoc.this.onTapListener.onMapTapped(geoPoint);
      
                          Location location = new Location("");
                          location.setLatitude((double) geoPoint.getLatitudeE6() / 1000000);
                          location.setLongitude((double) geoPoint.getLongitudeE6() / 1000000);
                          location.setAccuracy(Criteria.ACCURACY_FINE);
      
                          MapViewLoc.this.onTapListener.onMapTapped(location);
                      }
      
                      return true;
                  }
              };
          }
      
          public void addTapListener(OnTapListener onTapListener){
      
              this.prepareTagOverlay();
      
              this.getOverlays().add(0, this.tapOverlay);
      
              this.onTapListener = onTapListener;
          }
      
          public void removeTapListener(){
      
              if(this.tapOverlay != null && this.getOverlays().size() > 0){
      
                  this.getOverlays().remove(0);
              }
      
              this.tapOverlay = null;
              this.onTapListener = null;
          }
      
          public interface OnTapListener{
      
              void onMapTapped(GeoPoint geoPoint);
      
              void onMapTapped(Location location);
      
          }
      
      }
      

      要获取位置,只需设置接口 OnTapListener。

      mapView.addTapListener(new MapViewLoc.OnTapListener() {
      
                  @Override
                  public void onMapTapped(GeoPoint geoPoint) {}
      
                  @Override
                  public void onMapTapped(Location location) {
      
                      Toast toast = Toast.makeText(getApplicationContext(),
                              "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(),
                              Toast.LENGTH_SHORT);
      
                      toast.show();
                  }
              });
      

      【讨论】:

        【解决方案3】:

        您必须创建一个Overlay 并覆盖onSingleTapConfirmed

        试试这个:

        Overlay touchOverlay = new Overlay(this){
                ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = null;
                @Override
                protected void draw(Canvas arg0, MapView arg1, boolean arg2) {
        
                }
                @Override
                public boolean onSingleTapConfirmed(final MotionEvent e, final MapView mapView) {
        
                    final Drawable marker = getApplicationContext().getResources().getDrawable(R.drawable.markericon);
                    Projection proj = mapView.getProjection();
                    GeoPoint 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);
                    System.out.println("- Latitude = " + latitude + ", Longitude = " + longitude );
                    ArrayList<OverlayItem> overlayArray = new ArrayList<OverlayItem>();
                    OverlayItem mapItem = new OverlayItem("", "", new GeoPoint((((double)loc.getLatitudeE6())/1000000), (((double)loc.getLongitudeE6())/1000000)));
                    mapItem.setMarker(marker);
                    overlayArray.add(mapItem);
                    if(anotherItemizedIconOverlay==null){
                        anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
                        mapView.getOverlays().add(anotherItemizedIconOverlay);
                        mapView.invalidate();
                    }else{
                        mapView.getOverlays().remove(anotherItemizedIconOverlay);
                        mapView.invalidate();
                        anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(getApplicationContext(), overlayArray,null);
                        mapView.getOverlays().add(anotherItemizedIconOverlay);
                    }
              //      dlgThread();
                    return true;
                }
            };
            mapView.getOverlays().add(touchOverlay);
        

        【讨论】:

          【解决方案4】:

          试试下面的。

          编写一个派生自Overlay 类的类并覆盖onTap() 方法。然后您可以将叠加层添加到您的MapViewGeoPoint 对象,表示您点击的位置,当您在地图上的某个位置点击时,将传递给 onTap() 方法。

          【讨论】:

            【解决方案5】:

            @Sandy 是对的,但不是 onTap() 而是覆盖 onSingleTapUp(MotionEvent e, MapView mapView)onSingleTapConfirmed(MotionEvent e, MapView mapView) em> 方法,例如

            onSingleTapUp(MotionEvent e, MapView mapView) {
                Projection proj = mapView.getProjection();
                IGeoPoint p = proj.fromPixels(e.getX(), e.getY());
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-09-29
              • 1970-01-01
              • 1970-01-01
              • 2015-03-09
              • 2011-06-21
              • 2011-05-09
              相关资源
              最近更新 更多