简体   繁体   中英

How to listen to custom marker location change in Android?

I have a Geocoder IntentService that converts latitude and longitude to address. 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. 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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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