如何在android中拖动googleMap时获取标记位置

时间:2017-05-09 09:04:45

标签: android google-maps

在我的应用程序中,我在地图的布局中心设置了一个图像,我想拖动 googleMap 显示此图像的标记latLong

布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/mainToolbar"
        layout="@layout/toolbar_main" />

    <fragment
        android:id="@+id/mainMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/mainToolbar"
        tools:context="com.tida.utils.safine.Activities.MainActivity" />

    <ImageView
        android:id="@+id/location_setter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_location_origin_pin" />

</RelativeLayout>

Java代码:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private UiSettings mUiSettings;
    private CameraUpdate mapCenter, mapZoom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.mainMap);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mUiSettings = mMap.getUiSettings();
        mUiSettings.setScrollGesturesEnabled(true);
        mUiSettings.setZoomGesturesEnabled(true);
        mUiSettings.setTiltGesturesEnabled(true);
        mUiSettings.setRotateGesturesEnabled(true);

        /*Set center of Tehran location*/
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(35.724414, 51.427059), 18.0f));

        /*Map listener*/
        mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {
                Log.e("mapDrag", "DragStart : " + marker.getPosition());
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                Log.e("mapDrag", "Drag : " + marker.getPosition());
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                Log.e("mapDrag", "DragEnd : " + marker.getPosition());
            }
        });
    }
}

如何设置标记的图像并获得此位置?

2 个答案:

答案 0 :(得分:1)

marker.getPosition()返回LatLng

你应该使用:

marker.getPosition().latitude

&安培;

marker.getPosition().longitude

答案 1 :(得分:0)

使用标记的全局变量设置标记并调用mMarker.setPosition(marker.getPosition());