How to move a map under a marker?

旧城冷巷雨未停 提交于 2019-11-28 03:41:39
CommonSenseCode

Two easy steps:

Step 1

I used a RelativeLayout as parent of fragment with map and center in it an ImageView (which will do as the centered map marker just like in uber or easy taxi apps):

<!-- Map and ImageView in center for simulating the map marker -->
<RelativeLayout
    android:id="@+id/confirm_address_map_wrapper"
    android:layout_width="match_parent"
    android:layout_height="220dp">

    <fragment
        android:id="@+id/confirm_address_map_fragment"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        />
    <!-- Image View that acts as map marker notice centerInParent-->

     <View
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_centerInParent="true"/>


    <ImageView
        android:id="@+id/confirm_address_map_custom_marker"
        android:layout_width="@dimen/ICON_DEFAULT_SIZE"
        android:layout_height="@dimen/ICON_DEFAULT_SIZE"
        android:layout_above="@+id/view"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/my_map_marker"/>
</RelativeLayout>

Step 2

onMapReady() method at my activity I use googleMap instance method setOnCameraChangeListener() that lets me listen when the map is used and get latitude and longitude based on the center of camera position:

    @Override
    public void onMapReady(GoogleMap googleMap) {

        googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {

           Log.i("centerLat",cameraPosition.target.latitude);

           Log.i("centerLong",cameraPosition.target.longitude);
        }
    });

}

UPDATE:
OnCameraChangeListener is deprecated . Please use OnCameraIdleListener:
[Source: https://stackoverflow.com/a/38732283/421467]

I was not able to get the Uber app effect, however I was able to do something similar, perhaps it will do just fine for your solution making use of the GoogleMap.OnCameraChangeListener.

Activity:

public class MyActivity extends Activity implements OnCameraChangeListener {

    ...

    @Override
    public void onCameraChange(CameraPosition position) {
        mMap.clear();
        mMap.addMarker( new MarkerOptions()
            .position( position.target )
            .title( position.toString() )
        );
    }

    ...

}

The problem is, the marker is not drawn while the camera is being moved. However, it seems as though you have already created an overlay of a marker. So with an overlay it should work like the Uber application.

I hope this helps, even the question is a few months old, perhaps it will benefit future developers.

Kind regards Miki

If you are using Fragment to display your google map, put the ImageView inside the Fragment to overlay your customized marker such that your marker remains fixed when you are moving your map. Follow the code for xml file:

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" >

    <ImageView
        android:id="@+id/pin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@null"
        android:src="@drawable/pin" />
</fragment>

In the activity.java file use this:

ImageView pinButton = (ImageView) findViewById(R.id.pin);

use setOnCameraChangeListener() to get latitude and longitude based on the center of camera position or follow the answer provided by @androidPlusPlus

Sishin

Check this answer

first : add an imageview to the same layout and position equal to center. second: to get the coordinate of the center of the map use the following approach

1_ get viewgroup ( FrameLayout ) in my case where the map fragment is located.

 - FrameLayout myContainer = (FrameLayout) findViewById(R.id.content_frame);
    int mapHeight = myContainer.getHeight();
    int mapWidth = myContainer.getWidth();
2_ use setOnCameraChangeListener and on camera change get the coordinates:

LatLng center = mMap.getCameraPosition().target;

And also here is my complete solution

mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
    @Override
    public void onCameraMove() {
        mCurrLocationMarker.remove();
        LatLng midLatLng = mMap.getCameraPosition().target;
        mCurrLocationMarker = mMap.addMarker(new MarkerOptions().title("I am here ").
        position(midLatLng).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_point));
    }
});

mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
    @Override
    public void onCameraIdle() {
        mCurrLocationMarker.remove();
        LatLng position = mCurrLocationMarker.getPosition();
        mCurrLocationMarker = mMap.addMarker(new MarkerOptions().title("I am here ").
        position(position).icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_source)));
        getAddress(position.latitude, position.longitude);

    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!