简体   繁体   English

Java Android Google Maps更改我之前所在的颜色折线

[英]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. 我将此颜色绘制为黑色,但是当我在多段线上(在LatLtg中)时,我想将颜色更改为蓝色。 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 : 并检查第一个未完成的点是否在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. 您可以使用distanceBetween函数计算两个位置之间的近似距离(以米为单位),还可以计算两个位置之间最短路径的初始和最终方位。

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. 在这里,我将最小距离设置为50米。 these will return true if your current location(latlon) is within 50 miters from your polyline latlon. 如果您当前的位置(纬线)在多义线纬线的50个斜角以内,则返回true。

Define color value in colors.xml colors.xml定义颜色值

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 根据用户移动的androidgooglemap更新折线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM