matador0227
matador0227

Reputation: 131

googleMap onMarkerDrag not updating marker's .getPosition() Android Java

I am drawing polyLines between three markers on a GoogleMap, two of which are movable. I have implemented onMarkerDragListener to my fragment. moving just one of the markers 'seems' to work correctly, until I move the 2nd marker also. if I move the 2nd marker, the polyLine draws from the 2nd marker I'm moving to the original position of the 1st marker, not the updated one, which tells me that xMarker.getPosition() isn't getting updated when the marker is moved. how do I ensure xMarker's position is updated during the drag?

I tried setting it as an implementation at the class level of the fragment, and I also tried it after creating the map like so:

googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {

            }

            @Override
            public void onMarkerDrag(Marker marker) {

            }

            @Override
            public void onMarkerDragEnd(Marker marker) {

            }
        });

here is my code (currently using implements GoogleMap.OnMarkerListener):

        centerMarker = new MarkerOptions().position(
                        new LatLng(holeX, holeY)).title("CenterPoint");
                backTeeMarker = new MarkerOptions().position(new LatLng(backTeeX,backTeeY)).title("BackTee");
                flagMarker = new MarkerOptions().position(new LatLng(greenCenterX,greenCenterY)).title("flag");
        centerMarker.draggable(true);
                    backTeeMarker.draggable(true);
                    flagMarker.draggable(false);

                // adding marker
                googleMap.addMarker(centerMarker);
                googleMap.addMarker(backTeeMarker);
                googleMap.addMarker(flagMarker);

        backTeeToCenterPL = googleMap.addPolyline((new PolylineOptions())
                        .clickable(false)
                        .add(new LatLng(backTeeX, backTeeY),
                                new LatLng(centerMarker.getPosition().latitude, centerMarker.getPosition().longitude)
                        ));


                centerToFlagPL = googleMap.addPolyline((new PolylineOptions())
                .clickable(false)
                .add(new LatLng(centerMarker.getPosition().latitude,centerMarker.getPosition().longitude),
                            new LatLng(greenCenterX,greenCenterY)));

    @Override
        public void onMarkerDragStart(Marker marker) {

            markerMoved(marker);
        }

        @Override
        public void onMarkerDrag(Marker marker) {

            markerMoved(marker);


        }

        @Override
        public void onMarkerDragEnd(Marker marker) {
            markerMoved(marker);

        }

        public void markerMoved(Marker marker) {
            if (marker.getTitle().equals("CenterPoint")) {
                backTeeToCenterPL.remove();
                backTeeToCenterPL = googleMap.addPolyline((new PolylineOptions())
                        .clickable(false)
                        .add(new LatLng(backTeeMarker.getPosition().latitude, backTeeMarker.getPosition().longitude),
                                new LatLng(marker.getPosition().latitude, marker.getPosition().longitude)
                        ));
                backTeeToCenterPL.setColor(Color.WHITE);

                centerToFlagPL.remove();
                centerToFlagPL = googleMap.addPolyline((new PolylineOptions())
                        .clickable(false)
                        .add(new LatLng(marker.getPosition().latitude, marker.getPosition().longitude),
                                new LatLng(greenCenterX, greenCenterY)));
                centerToFlagPL.setColor(Color.WHITE);
}
}
        if (marker.getTitle().equals("BackTee")) {
            backTeeToCenterPL.remove();
            backTeeToCenterPL = googleMap.addPolyline((new PolylineOptions())
                    .clickable(false)
                    .add(new LatLng(marker.getPosition().latitude, marker.getPosition().longitude),
                            new LatLng(centerMarker.getPosition().latitude, centerMarker.getPosition().longitude)
                    ));
            backTeeToCenterPL.setColor(Color.WHITE);

            centerToFlagPL.remove();
            centerToFlagPL = googleMap.addPolyline((new PolylineOptions())
                    .clickable(false)
                    .add(new LatLng(centerMarker.getPosition().latitude, centerMarker.getPosition().longitude),
                            new LatLng(greenCenterX, greenCenterY)));
            centerToFlagPL.setColor(Color.WHITE);
}

Upvotes: 0

Views: 324

Answers (1)

matador0227
matador0227

Reputation: 131

when moving the marker, I expected the centerMarker.position and backTeeMarker.position would be updated automatically by googleMap, given that it is drawing it in a new place, it must have an understanding of it's new location. however, this turned out not to be the case.

It seems that it updates the moved 'marker' position within the onMarkerDragListener, but never translates it to the named Marker object's position. and so to update it manually with the markerMoved() method, when I was moving the given marker, for example centerMarker, I had to add the following line: centerMarker.position(marker.getPosition()).

Upvotes: 1

Related Questions