Android: Drag map while keeping marker at the center

后端 未结 2 425
别跟我提以往
别跟我提以往 2020-12-09 06:00

I am currently getting Current Location of device (lat,lng) in MapActivity. Now , I want to let the user move the map, while keeping the marker at the center. This means tha

相关标签:
2条回答
  • 2020-12-09 06:25

    Having a map under an ImageView with your marker icon would be the best way to go here (just a framelayout with your map fragment under and your marker image centered on top should work). Then you can use an OnCameraIdleListener which would receive a callback when the map movement has ended. So just set an instance of OnCameraIdleListener to your map to listen for movement callbacks like so:

    map.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
            @Override
            public void onCameraIdle() {
                //get latlng at the center by calling
                LatLng midLatLng = map.getCameraPosition().target;
            }
        });
    
    0 讨论(0)
  • 2020-12-09 06:34

    Try at first this layout for your Activity_map.xml:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment"
        />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/gps"
        />
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题