How do I get the coordinates of a map on tap with MapFragment (not MapView)?

前端 未结 1 503
不知归路
不知归路 2021-02-20 08:22

I have searched around on how to get the coordinates of a location when the map is tapped. However, most, if not all the examples require a MapView as a parameter.

相关标签:
1条回答
  • 2021-02-20 09:01

    There is an example in the Sample Code provided by Google Play services SDK. This uses SupportMapFragment so I'm not sure how helpful this will be if you are using the new MapFragment.

    The method the EventsDemoActivity uses in this map sample code is to implement OnMapClickListener to the class. Below is some of the code you might be able to use.

    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>
    
    0 讨论(0)
提交回复
热议问题