【问题标题】:How do I get the coordinates of a map on tap with MapFragment (not MapView)?如何使用 MapFragment(不是 MapView)获取地图的坐标?
【发布时间】:2013-02-28 00:40:41
【问题描述】:

我已经搜索了如何在点击地图时获取位置的坐标。但是,大多数(如果不是所有)示例都需要 MapView 作为参数。例如:

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 在地图上获取点击位置的位置?

【问题讨论】:

    标签: android location touch-event google-maps-android-api-2 mapfragment


    【解决方案1】:

    Google Play 服务 SDK 提供的Sample Code 中有一个示例。这使用SupportMapFragment,所以我不确定如果您使用新的MapFragment,这会有多大帮助。

    EventsDemoActivity 在此地图示例代码中使用的方法是 implement OnMapClickListener 到类。下面是一些您可能可以使用的代码。

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      相关资源
      最近更新 更多