Mili Maric
Mili Maric

Reputation: 53

Android google maps marker constantly clicking on the same point

When I click somewhere on map, app draw polyline between current location and destination (click). I want that destination marker is clicking by himself all the time (every few seconds), so on user movement new polylines are drawing (updating the last one).

I tried with handler and bunch of the things and didn't find solution.

Any answer would be highly appreciated.

private ArrayList<LatLng> mClickedPoints;

/** Setting onclick listener for the map */
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {

        /**
         *  Clearing map if user clicks on map more then one time.
         * Reseting View widgets and arrays, adding points of interests
         */
        if (mClickedPoints.size() > 1) {
            mMap.clear();
            mClickedPoints.clear();
            mClickedPoints = new ArrayList<>();
        }

        /**  Adding current location to the ArrayList */
        mClickedPoints.add(start);
        Log.i("current location", start.toString());

        MarkerOptions options = new MarkerOptions();
        options.position(start);

        /** Destination click */
        mClickedPoints.add(point);

        /** Setting the position of the marker */
        options.position(point);

        /**  Checks if start and end locations are captured */
        if (mClickedPoints.size() >= 2) {
            orig = mClickedPoints.get(0);
            dest = mClickedPoints.get(1);
        }
        mMap.addMarker(options);
        request_directions_and_get_response();
    }
});

onLocationChanged() override:

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    /** Setting current location marker */
    start = new LatLng(location.getLatitude(),location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(start);
    markerOptions.title("Current Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mCurrLocationMarker = mMap.addMarker(markerOptions);
}

Upvotes: 3

Views: 447

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

In order to use the clicked points ArrayList as you have it now, you'll need to remove the previous origin, then add the current origin to the start of the ArrayList, and then get re-calculated directions based on the new current location.

Something like this:

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    /** Setting current location marker */
    start = new LatLng(location.getLatitude(),location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(start);
    markerOptions.title("Current Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    mCurrLocationMarker = mMap.addMarker(markerOptions);

    //If there is a previous route drawn:
    if (mClickedPoints.size() >= 2) {
        //remove the old origin:
        mClickedPoints.remove(0);

        //add the new one at the 0th position:
        mClickedPoints.add(0, start);

        //set orig and dest for directions:
        orig = mClickedPoints.get(0);
        dest = mClickedPoints.get(1);

        //get updated directions:
        request_directions_and_get_response();
    }

}

Upvotes: 1

Related Questions