繁体   English   中英

如何在Android中收听自定义标记位置的更改?

[英]How to listen to custom marker location change in Android?

我有一个Geocoder IntentService,可将纬度和经度转换为地址。 我还在屏幕的正中间有一个自定义标记图像。 我想通过简单地拖动地图并将其放置在我的自定义标记下,让用户选择他们想要的任何位置。 这样做之后,我想获取屏幕中心的纬度和经度,并将这些参数传递给Geocoder类。 我尝试了以下操作,但仅获得当前位置。 标记卡在中间,这意味着如果我拖动地图,它会弹回到我的当前位置。 我正在使用Google Maps 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 ,然后调用onCameraChange以及Geocoder函数。

样例代码:

<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>

有关更多详细信息,请参阅此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM