如何监听 Android 中的自定义标记位置变化?

How to listen to custom marker location change in Android?

我有一个 Geocoder IntentService 可以将纬度和经度转换为地址。我还在屏幕的正中间有一个自定义标记图像。我想让用户 select 通过简单地拖动地图并将其放置在我的自定义标记下方来让他们想要的任何位置。一旦他们这样做,我想获得屏幕中心的纬度和经度并将这些参数传递给 Geocoder class。我尝试了以下但我只得到了当前位置。标记卡在中间,这意味着如果我拖动地图,它会弹回我当前的位置。我正在使用 Google 地图 api V2.

protected void startGeocoderService(final Location lo) {
        map.setOnCameraChangeListener(new OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
        try {
            LatLng lalng = new LatLng(lo.getLatitude(), lo.getLongitude());
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(lalng, 15));
            LatLng newloatlng = map.getCameraPosition().target;
            lo.setLatitude(newloatlng.latitude);
            lo.setLongitude(newloatlng.longitude);

                Intent myintent = new Intent(MapActivity.this, FetchAddressIntentService.class);
                myintent.putExtra(Constants.RECEIVER, mAddressResultReceiver);
                myintent.putExtra(Constants.LOCATION_DATA_EXTRA, lo);
                startService(myintent);

            }catch (NullPointerException nullexception){
                nullexception.printStackTrace();
            }

            }
        });
    }

我怎样才能做到这一点?预先感谢大家的帮助

我认为您可以使用 FrameLayout 为您的地图和标记设置 overlap effect,然后调用 onCameraChangeGeocoder 函数。

示例代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

        <LinearLayout
            android:id="@+id/locationMarker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="30dp"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/locationMarkertext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_corner_map"
                android:gravity="center"
                android:minWidth="180dp"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:text=" Set your Location "
                android:textColor="@android:color/white" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/add_marker" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Selected Location"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#28b54c" />

            <TextView
                android:id="@+id/adressText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="Getting location"
                android:textSize="16sp" />
        </LinearLayout>
    </FrameLayout>

</RelativeLayout>

详情请参考here