2017-06-14 70 views
-1

第一次制作android应用程序,并尝试将Google方向的多段线添加到Google地图上。使用LatLng谷歌地图的不兼容类型

进口了: com.google.android.gms.maps.model.LatLng

我想折线点进行解码。 尽量选用decode从PolylineEncoding类,但是从这个进口: com.google.maps.model.LatLng

这将导致不兼容的类型,那么你如何确保你使用的是兼容的类型?或者其他的方式来解码这个多段线到一个特定的LatLng类型而不重写算法?

+0

你想让路线从一个地方到另一个地方 – SSALPHAX

+0

@SSALPHAX亚!我试图弄清楚我应该为此使用什么。 现在使用PolyUtil对其进行解码 so – oppnahar

回答

0

试试这个通过你的结果在drawPath()方法

public void drawPath(String result) { 
    if (line != null) { 
     googleMap.clear(); 
    } 
    googleMap.addMarker(new MarkerOptions().position(Dloca)); 
    //googleMap.addMarker(new MarkerOptions().position(loc)); 
    try { 
     // Tranform the string into a json object 
     final JSONObject json = new JSONObject(result); 
     JSONArray routeArray = json.getJSONArray("routes"); 
     JSONObject routes = routeArray.getJSONObject(0); 
     JSONObject overviewPolylines = routes 
       .getJSONObject("overview_polyline"); 
     String encodedString = overviewPolylines.getString("points"); 
     List<LatLng> list = decodePoly(encodedString); 

     for (int z = 0; z < list.size() - 1; z++) { 
      LatLng src = list.get(z); 
      LatLng dest = list.get(z + 1); 
      line = googleMap.addPolyline(new PolylineOptions() 
        .add(new LatLng(src.latitude, src.longitude), 
          new LatLng(dest.latitude, dest.longitude)) 
        .width(5).color(Color.BLUE).geodesic(true)); 
     } 

     dialog.dismiss(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private List<LatLng> decodePoly(String encoded) { 

    List<LatLng> poly = new ArrayList<LatLng>(); 
    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))); 
     poly.add(p); 
    } 

    return poly; 
} 

该代码将返回根据您的路由源和目标位置你谷歌地图线。

+0

所以我想避免重写这种方法,因为当它是地图api的一部分时会是什么点。 我不认为我的问题很明确,但我最初的问题是理解Google软件包为什么使用不同的LatLng类型。最后,我使用了类似于: '进口com.google.maps.android.PolyUtil 进口com.google.android.gms.maps.model.LatLng ... 列表方向= PolyUtil.decode( polylineString); PolylineOptions p = new PolylineOptions(); p.addAll(directions); googleMap.addPolyline(p); ''' – oppnahar