在 Android Google 地图中将折线与道路边缘对齐

Align Polyline to Edge of Road in Android Google Maps

我在网上看到这个 所以我确认可以在Google Maps.

中的一个路径中有2个填充2个不同的折线

为了对此进行实验,我尝试首先使用以下代码仅添加一条折线:

public static void drawEncodedPolyOnMap(String encoded, GoogleMap map) {
    List<LatLng> points = new ArrayList<>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;
    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        LatLng p = new LatLng((((double) lat / 1E5)),
            (((double) lng / 1E5)));
        points.add(p);
    }

    map.addPolyline(new PolylineOptions().width(7).color(Color.RED).geodesic(true).addAll(points));
}

但它没有给我预期的显示效果

基本上在我的情况下,即使标记位于道路边缘,多段线也会居中对齐,我不希望发生这种情况。

是否可以将多段线与 Android 中的标记对齐?怎么样?

似乎没有人回答,我只是找到了一种方法。

基本上,我刚刚更新了我的 drawEncodedPolyOnMap 以向 bit shifting 返回的原始 LatLng 添加填充。下面是代码

public static void drawEncodedPolyOnMap(String encoded, GoogleMap map, boolean type) {
    List<LatLng> points = new ArrayList<>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;
    while (index < len) {
        int b, shift = 0, result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        double newLat = (((double) lat / 1E5));
        double newLng = (((double) lng / 1E5));

        if(type){
            points.add(new LatLng(newLat+PADDING_LAT, newLng+PADDING_LNG));
        }else{
            points.add(new LatLng(newLat-PADDING_LAT, newLng-PADDING_LNG));
        }
    }

    map.addPolyline(new PolylineOptions().width(7).color((type)?Color.RED:Color.GREEN).geodesic(true).addAll(points));
}

就是这样。问题已解决。