简体   繁体   中英

Java android google maps change color polyline where I was

This is how I draw a polyline :

 List<LatLng> latLngsList = new ArrayList<>();
            for (LegsItem leg : response.body().getRoutes().get(0).getLegs()) {
                for (StepsItem step : leg.getSteps()) {
                    List<LatLng> latLngs = PolyUtil.decode(step.getGeometry());
                    step.setLatLngs(latLngs);
                    latLngsList.addAll(latLngs);
                }
            }

            Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                    .addAll(latLngsList));

I draw this on color black but when I am on the polyline(In LatLtg), I want to change the color to blue. To detect if I am on the point, I use the below:

mMap.setOnMyLocationChangeListener 

and check if the first not done point is near than 2 m :

double dis = currentLocation.distanceTo(location);

But it does not work correctly

You can use the function distanceBetween which Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.

public static boolean checkDistance(LatLng oldPosition, LatLng newPosition) {
        float[] results = new float[1];
        Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
                newPosition.latitude, newPosition.longitude, results);
        return results[0] <= 50.0;
    }

Here i set minimum distance 50 meter. these will return true if your current location(latlon) is within 50 miters from your polyline latlon.

Define color value in colors.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#0000EE</color>
</resources>

and set color like this :-

Polyline line = mMap.addPolyline(new PolylineOptions()
                .addAll(list)
                .color(R.color.blue));

EDIT

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {

                for (LatLng nLatLng : latLngsList) {

                    double latitude = nLatLng.latitude;
                    double longitude = nLatLng.longitude;
                    double myLat = location.getLatitude();
                    double myLong = location.getLongitude();

                    if (latitude == myLat && longitude == myLong) {


                        Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                                .add(nLatLng)
                                .color(R.color.blue));
                    } /*else {
                        Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                                .add(nLatLng)
                                .color(R.color.black));
                    }*/
                }


            }
        });

update-polyline-according-to-the-user-moving-android-googlemaps

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