【问题标题】:Get the distance between two locations like in the http request in Google Directions获取两个位置之间的距离,如 Google Directions 中的 http 请求
【发布时间】:2013-07-25 22:18:05
【问题描述】:

我知道关于这个问题有很多话题,但我的问题更具体。 我调用 Google Directions http API 来获取两个位置之间的路由。响应的 JSON 包含如何从 A 点行进到 B 点的所有信息。但是我如何自己计算距离,例如在我移动时我的实际位置和一个步骤的下一个结束位置之间的距离?我需要的是“公路线路”而不是“航空线路”。

我所做的是使用 LocationManager 获取我的实际位置的 lat、lng。有了这个和最终位置的 lat, lng,我调用 Location.distanceBetween 方法。但它给出了一个不精确的距离。不精确意味着 distancebetween 方法的结果与方向 API 的距离不同。长的路,短的路。

一种解决方案是多次调用 Directions API...但必须有更好的解决方法才能获得沿道路的实际距离。有什么工具可以得到我想要的吗?

经过编辑使其更易于理解

【问题讨论】:

    标签: android location directions


    【解决方案1】:

    调用 Directions API:

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

    之后,您可以在文档中收到的所有 json 对象上运行,并检查每对点之间的距离:

      public int getDistanceValue (Document doc) {
        NodeList nl1 = doc.getElementsByTagName("distance");
        Node node1 = nl1.item(0);
        NodeList nl2 = node1.getChildNodes();
        Node node2 = nl2.item(getNodeIndex(nl2, "value"));
        Log.i("DistanceValue", node2.getTextContent());
        return Integer.parseInt(node2.getTextContent());
    }
    

    这是获得所有分数的方法:

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

    其实我用这段代码在地图上画了一个Polyline的方向,但是距离函数应该也可以。

    【讨论】:

    • 您的代码描述了如何从 Directions API 响应中获取路由信息。但是我需要在移动时从我的实际位置从 API 生成步长/折线的距离值。
    猜你喜欢
    • 2012-12-12
    • 2013-08-21
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2013-09-26
    相关资源
    最近更新 更多