简体   繁体   中英

How can I get position from a draggable marker in Google Maps?

This is my current method

private void createNewMarker() {
    MarkerOptions AlarmMarker = new MarkerOptions();
    AlarmMarker.position(SearchedLocation).alpha(2).draggable(true)
            .snippet(MessageDataText).title(MessageDataTitle).zIndex(2.0f);
    mMap.addMarker(AlarmMarker);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(SearchedLocation,10));
    mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDrag(@NonNull Marker marker) {

        }

        @Override
        public void onMarkerDragEnd(@NonNull Marker marker) {
            AlarmMarkerLatLng = AlarmMarker.getPosition();
            Toast.makeText(getApplicationContext(), AlarmMarkerLatLng.toString(), Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onMarkerDragStart(@NonNull Marker marker) {

        }
    });


}

This only returns position that my marker was created on and not the updated one...

On your setOnMarkerDragListener, you want to use the local variable 'marker'. Don't try to get the value from outside of the scope.

mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDrag(@NonNull Marker marker) {

        }

        @Override
        public void onMarkerDragEnd(@NonNull Marker marker) {
            Toast.makeText(getApplicationContext(), marker.getPostition.toString(), Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onMarkerDragStart(@NonNull Marker marker) {

        }
    });

The documentation has some really good examples: https://developers.google.com/maps/documentation/android-sdk/marker

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