Android google maps marker constantly clicking on the same point

When I click on the map, the application picks up the polyline between the current location and the destination (click). I want the destination marker to click on it all the time (every few seconds), so new polylines appear when the user moves (updating the last one).

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

Any answer would be much 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);
}

      

+3


source to share


1 answer


To use the clicked points of the ArrayList as you do now, you need to remove the previous origin, then add the current origin to the origin of the ArrayList, and then recalculate the routes based on the new current location.

Something like that:



@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();
    }

}

      

+1


source







All Articles