简体   繁体   English

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

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

I have a Geocoder IntentService that converts latitude and longitude to address. 我有一个Geocoder IntentService,可将纬度和经度转换为地址。 I also have a custom marker image in the exact middle of the screen. 我还在屏幕的正中间有一个自定义标记图像。 I would like to let the user select any location they want by simply dragging the map and place it right under my custom marker. 我想通过简单地拖动地图并将其放置在我的自定义标记下,让用户选择他们想要的任何位置。 As soon as they do that I would like to get the latitude and longitude of the center of the screen and pass these arguments to the Geocoder class. 这样做之后,我想获取屏幕中心的纬度和经度,并将这些参数传递给Geocoder类。 I tried the following but I only get the current location. 我尝试了以下操作,但仅获得当前位置。 And the marker is stuck in the middle, meaning if I drag the map it bounces back to my current location. 标记卡在中间,这意味着如果我拖动地图,它会弹回到我的当前位置。 I am using Google maps api V2. 我正在使用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();
            }

            }
        });
    }

How can I accomplish that? 我该怎么做? Thank you in advance everyone for your help 预先感谢大家的帮助

I think you can use FrameLayout to have the overlap effect for your map and markers and then call the onCameraChange as well as Geocoder function. 我认为您可以使用FrameLayout对地图和标记具有overlap effect ,然后调用onCameraChange以及Geocoder函数。

Sample code: 样例代码:

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

For more details, please refer here . 有关更多详细信息,请参阅此处

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

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