11

地図がタップされたときに場所の座標を取得する方法を調べました。ただし、ほとんどの例ではMapView、パラメーターとして a が必要です。例えば:

public boolean onTap(GeoPoint p, MapView map){
    if ( isPinch ){
        return false;
    }else{
        Log.i(TAG,"TAP!");
        if ( p!=null ){
            handleGeoPoint(p);
            return true;            // We handled the tap
        }else{
            return false;           // Null GeoPoint
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView)
{
    int fingers = e.getPointerCount();
    if( e.getAction()==MotionEvent.ACTION_DOWN ){
        isPinch=false;  // Touch DOWN, don't know if it's a pinch yet
    }
    if( e.getAction()==MotionEvent.ACTION_MOVE && fingers==2 ){
        isPinch=true;   // Two fingers, def a pinch
    }
    return super.onTouchEvent(e,mapView);
}

したがって、マップ上のタップされた位置の位置を取得するにはどうすればよいMapFragmentですMapViewか?

4

1 に答える 1

27

Google Play サービス SDK が提供するサンプル コードに例があります。これは を使用してSupportMapFragmentいるため、新しい を使用している場合にこれがどれほど役立つかはわかりませんMapFragment

このマップ サンプル コードで EventsDemoActivity が使用するメソッドは、implement OnMapClickListenerto クラスです。以下は、使用できる可能性のあるコードの一部です。

EventsDemoActivity:

public class EventsDemoActivity extends FragmentActivity
    implements OnMapClickListener, OnMapLongClickListener {

    private GoogleMap mMap;
    private TextView mTapTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events_demo);

        mTapTextView = (TextView) findViewById(R.id.tap_text);

        setUpMapIfNeeded();
    }

    private void setUpMap() //If the setUpMapIfNeeded(); is needed then...
    {
        mMap.setOnMapClickListener(this);
        mMap.setOnMapLongClickListener(this);
    }

    @Override
    public void onMapClick(LatLng point) {
        mTapTextView.setText("tapped, point=" + point);
    }

    @Override
    public void onMapLongClick(LatLng point) {
        mTapTextView.setText("long pressed, point=" + point);
    }
}


events_demo.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <TextView
    android:id="@+id/tap_text"
    android:text="@string/tap_instructions"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
  <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
于 2013-03-12T15:01:09.983 に答える