2017-03-04 92 views
0

我在網絡中看到了這個image 所以我確認可以在Google地圖的一個路徑中填寫2條不同的折線。在Google地圖中將折線對齊到道路邊緣

要試驗這個,我想先使用此代碼只添加一個折線:

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

但它不給我,我預期

enter image description here

基本上顯示在我的情況下,即使標記位於道路邊緣,多段線也會居中對齊,這種情況我不喜歡發生。

是否可以將折線與Android中的標記對齊?怎麼樣?

回答

0

似乎沒有人回答,我只是找到了一個方法如何做到這一點。

基本上,我剛剛更新了我的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)); 
} 

就是這樣。問題解決了。