2017-08-31 74 views
0

你能告诉我如何获得在谷歌地图android中的点之间的折线距离?如何在android中的谷歌地图中查找多段线的距离?

map.addPolyline(new PolylineOptions() 
.add(new LatLng(75.5, -1.1), new LatLng(60.7, -84.0), 80.7, -32.0)) 
.width(5) 
.color(Color.BLUE)); 

在上面的代码中,我绘制了一条使用折线的路径。所以我想知道如何计算ot得到这条折线的距离。我知道有一些方法,如“distanceTo”。这些方法为您提供点到点之间的距离。但在我的情况下,我有几点,你可以看到在上面的例子中,绘制一条折线。

+0

你想找到多段点之间的距离吗? –

回答

0

试试这个:

Polyline polylin; 
ArrayList<LatLng> directionPoint; 
PolylineOptions rectLine; 
Document doc; 
GMapV2Direction md; 

public class showRoad extends AsyncTask<Void,Void,Document>{ 

    @Override 
    protected Document doInBackground(Void... params) { 
     md = new GMapV2Direction(); 
     doc = md.getDocument(lastLocation, univ_position, 
       GMapV2Direction.MODE_DRIVING); 

     directionPoint = md.getDirection(doc); 

     rectLine = new PolylineOptions().width(10).color(
       Color.BLUE); 

     for (int i = 0; i < directionPoint.size(); i++) { 
      rectLine.add(directionPoint.get(i)); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Document document) { 
     polylin = mMap.addPolyline(rectLine); 
    } 
} 

,并称之为:

new showRoad().execute(); 

最后加入这个类:

public class GMapV2Direction { 
public final static String MODE_DRIVING = "driving"; 
public final static String MODE_WALKING = "walking"; 

public GMapV2Direction() { 
} 

public Document getDocument(LatLng start, LatLng end, String mode) { 
    String url = "http://maps.googleapis.com/maps/api/directions/xml?" 
      + "origin=" + start.latitude + "," + start.longitude 
      + "&destination=" + end.latitude + "," + end.longitude 
      + "&sensor=false&units=metric&mode=driving"; 
    Log.d("url", url); 
    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
       .newDocumentBuilder(); 
     Document doc = builder.parse(in); 
     return doc; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

public String getDurationText(Document doc) { 
    try { 

     NodeList nl1 = doc.getElementsByTagName("duration"); 
     Node node1 = nl1.item(0); 
     NodeList nl2 = node1.getChildNodes(); 
     Node node2 = nl2.item(getNodeIndex(nl2, "text")); 
     Log.i("DurationText", node2.getTextContent()); 
     return node2.getTextContent(); 
    } catch (Exception e) { 
     return "0"; 
    } 
} 

public int getDurationValue(Document doc) { 
    try { 
     NodeList nl1 = doc.getElementsByTagName("duration"); 
     Node node1 = nl1.item(0); 
     NodeList nl2 = node1.getChildNodes(); 
     Node node2 = nl2.item(getNodeIndex(nl2, "value")); 
     Log.i("DurationValue", node2.getTextContent()); 
     return Integer.parseInt(node2.getTextContent()); 
    } catch (Exception e) { 
     return -1; 
    } 
} 

public String getDistanceText(Document doc) { 
    /* 
    * while (en.hasMoreElements()) { type type = (type) en.nextElement(); 
    * 
    * } 
    */ 

    try { 
     NodeList nl1; 
     nl1 = doc.getElementsByTagName("distance"); 

     Node node1 = nl1.item(nl1.getLength() - 1); 
     NodeList nl2 = null; 
     nl2 = node1.getChildNodes(); 
     Node node2 = nl2.item(getNodeIndex(nl2, "value")); 
     Log.d("DistanceText", node2.getTextContent()); 
     return node2.getTextContent(); 
    } catch (Exception e) { 
     return "-1"; 
    } 

    /* 
    * NodeList nl1; if(doc.getElementsByTagName("distance")!=null){ nl1= 
    * doc.getElementsByTagName("distance"); 
    * 
    * Node node1 = nl1.item(nl1.getLength() - 1); NodeList nl2 = null; if 
    * (node1.getChildNodes() != null) { nl2 = node1.getChildNodes(); Node 
    * node2 = nl2.item(getNodeIndex(nl2, "value")); Log.d("DistanceText", 
    * node2.getTextContent()); return node2.getTextContent(); } else return 
    * "-1";} else return "-1"; 
    */ 
} 

public int getDistanceValue(Document doc) { 
    try { 
     NodeList nl1 = doc.getElementsByTagName("distance"); 
     Node node1 = null; 
     node1 = nl1.item(nl1.getLength() - 1); 
     NodeList nl2 = node1.getChildNodes(); 
     Node node2 = nl2.item(getNodeIndex(nl2, "value")); 
     Log.i("DistanceValue", node2.getTextContent()); 
     return Integer.parseInt(node2.getTextContent()); 
    } catch (Exception e) { 
     return -1; 
    } 
    /* 
    * NodeList nl1 = doc.getElementsByTagName("distance"); Node node1 = 
    * null; if (nl1.getLength() > 0) node1 = nl1.item(nl1.getLength() - 1); 
    * if (node1 != null) { NodeList nl2 = node1.getChildNodes(); Node node2 
    * = nl2.item(getNodeIndex(nl2, "value")); Log.i("DistanceValue", 
    * node2.getTextContent()); return 
    * Integer.parseInt(node2.getTextContent()); } else return 0; 
    */ 
} 

public String getStartAddress(Document doc) { 
    try { 
     NodeList nl1 = doc.getElementsByTagName("start_address"); 
     Node node1 = nl1.item(0); 
     Log.i("StartAddress", node1.getTextContent()); 
     return node1.getTextContent(); 
    } catch (Exception e) { 
     return "-1"; 
    } 

} 

public String getEndAddress(Document doc) { 
    try { 
     NodeList nl1 = doc.getElementsByTagName("end_address"); 
     Node node1 = nl1.item(0); 
     Log.i("StartAddress", node1.getTextContent()); 
     return node1.getTextContent(); 
    } catch (Exception e) { 
     return "-1"; 
    } 
} 
public String getCopyRights(Document doc) { 
    try { 
     NodeList nl1 = doc.getElementsByTagName("copyrights"); 
     Node node1 = nl1.item(0); 
     Log.i("CopyRights", node1.getTextContent()); 
     return node1.getTextContent(); 
    } catch (Exception e) { 
     return "-1"; 
    } 

} 

public ArrayList<LatLng> getDirection(Document doc) { 
    NodeList nl1, nl2, nl3; 
    ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>(); 
    nl1 = doc.getElementsByTagName("step"); 
    if (nl1.getLength() > 0) { 
     for (int i = 0; i < nl1.getLength(); i++) { 
      Node node1 = nl1.item(i); 
      nl2 = node1.getChildNodes(); 

      Node locationNode = nl2 
        .item(getNodeIndex(nl2, "start_location")); 
      nl3 = locationNode.getChildNodes(); 
      Node latNode = nl3.item(getNodeIndex(nl3, "lat")); 
      double lat = Double.parseDouble(latNode.getTextContent()); 
      Node lngNode = nl3.item(getNodeIndex(nl3, "lng")); 
      double lng = Double.parseDouble(lngNode.getTextContent()); 
      listGeopoints.add(new LatLng(lat, lng)); 

      locationNode = nl2.item(getNodeIndex(nl2, "polyline")); 
      nl3 = locationNode.getChildNodes(); 
      latNode = nl3.item(getNodeIndex(nl3, "points")); 
      ArrayList<LatLng> arr = decodePoly(latNode.getTextContent()); 
      for (int j = 0; j < arr.size(); j++) { 
       listGeopoints.add(new LatLng(arr.get(j).latitude, arr 
         .get(j).longitude)); 
      } 

      locationNode = nl2.item(getNodeIndex(nl2, "end_location")); 
      nl3 = locationNode.getChildNodes(); 
      latNode = nl3.item(getNodeIndex(nl3, "lat")); 
      lat = Double.parseDouble(latNode.getTextContent()); 
      lngNode = nl3.item(getNodeIndex(nl3, "lng")); 
      lng = Double.parseDouble(lngNode.getTextContent()); 
      listGeopoints.add(new LatLng(lat, lng)); 
     } 
    } 

    return listGeopoints; 
} 

private int getNodeIndex(NodeList nl, String nodename) { 
    for (int i = 0; i < nl.getLength(); i++) { 
     if (nl.item(i).getNodeName().equals(nodename)) 
      return i; 
    } 
    return -1; 
} 

private ArrayList<LatLng> decodePoly(String encoded) { 
    ArrayList<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 position = new LatLng((double) lat/1E5, (double) lng/1E5); 
     poly.add(position); 
    } 
    return poly; 
} 

}

0

我已经在我的一个项目中完成了这项工作......我不确定这可以帮助您,但对于找到距离折线的距离,您可以使用包含一个方法distanceToLine的PolyUtil类。

dependencies { 
    compile 'com.google.maps.android:android-maps-utils:0.5+' 
} 

将此库添加到您的build.gradle中。欲了解更多,您可以使用谷歌地图工具class.This库是由谷歌提供,你可以在这里找到

https://developers.google.com/maps/documentation/android-api/utility/

你可以找到一些在这里PolyUtil方法描述...

http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/PolyUtil.html#isLocationOnPath-LatLng-java.util.List-boolean-double-

希望这会帮助你..如果有任何问题,请通知我..我可以帮你。