2013-05-21 10 views
7

開いた道路地図でポイントの座標をクリックすると、どのようにポイントを取得できますか?地図をクリックして座標を取得する(openstreetmaps)

は試してみました:

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

歓声、 Thanasio

答えて

9

使用dispatchTouchEvent()方法を。 MapActivitydispatchTouchイベントを継承し、アクティビティクラスの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); 
} 
0

以下を試してください。

Overlayクラスから派生し、onTap()メソッドをオーバーライドするクラスを作成します。次に、あなたのオーバーレイをに追加することができます。タップした位置を表すGeoPointオブジェクトは、マップのどこかをタップするとonTap()メソッドに渡されます。

0

@Sandyが正しい代わりONTAP()オーバーライドonSingleTapUp(MotionEvent電子、のMapViewのMapView)又はonSingleTapConfirmed(MotionEvent電子、のMapView用のMapView)方法の、例えば

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

このマップをクリックして位置を取得するための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(); 
      } 
     }); 
1

あなたが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); 
関連する問題