0

I'm working on Android Studio>> I have an origin point and destiny point. I draw the route with the JSONData from Google Services and I create a polyline. Now, I need to monitor user's current position from the polyline in the map, but I can't.

Here's my code:

private void drawRoute(List<LatLng> list) {
    if ((list != null) && (list.size() > 0)) {
        polylineOptions = new PolylineOptions()
                .addAll(list)
                .width(8);
        map.addPolyline(polylineOptions);

        CircleOptions circleOptions;
        for (int i = 0; i < polylineOptions.getPoints().size(); i++) {
            LatLng point = polylineOptions.getPoints().get(i);
            circleOptions = new CircleOptions()
                    .center(new LatLng(point.latitude, point.longitude))
                    .strokeColor(Color.TRANSPARENT)
                    .strokeWidth(1)
                    .fillColor(Color.argb(100, 164, 171, 167))
                    .radius(10);
            map.addCircle(circleOptions);
        }
    } else {
        Log.i("Msg", "No routes");
    }
}

And I get the distance with this:

private double getDistance(LatLng originPoint, LatLng destinyPoint) {
    try {
        Location origin = new Location("Origin point");
        origin.setLatitude(originPoint.latitude);
        origin.setLongitude(originPoint.longitude);
        Location destiny = new Location("Destiny point");
        destiny.setLatitude(destinyPoint.latitude);
        destiny.setLongitude(destinyPoint.longitude);
        return origin.distanceTo(destiny);
    } catch (Exception e) {
        Log.e("Msg", e.getMessage());
        return 0d;
    }
}

I don't know if there's a way to find a "piece" of polyline in the circle around user's current location and calculate the distance between that "piece" and current location. I've been searching but the code I found is in V3 and I'm starting Android apps. I appreciate your help!

2 Answers2

0

Well, if you need it, I resolve it with this code:

    @Override
    protected Boolean doInBackground(List<LatLng>... params) {
        publishProgress();
        List<LatLng> list = params[0];
        LatLng currentPosition = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (obtenerDistancia(currentPosition, list.get(i)) <= 100) {
                return false;
            }
        }
        return true;
    }

Where list = current position + polyline.getPoints()

list[0] = current position;

list[1], list[2]..., list[n] = polyline points.

I calculate distance between my current position (list[0]) and each point in polyline (list[1], list[2]..., list[n]]. If I find at least one point that is <= 100 meters from my current position, that means user is close to the drawn route.

0

You don't need to find the "piece" around the user current postion. You just need to decodePolyLine the points in the JSon file and put all the LatLng to a list and use the class I have created to check it. Here is my answer for the relative topic it have the Class and the method you're looking for: https://stackoverflow.com/a/49904276/7548514

Kyo Huu
  • 520
  • 7
  • 13