【问题标题】:How to get shortest driving path and distance between two geopoint coordinates?如何获得两个地理点坐标之间的最短行驶路径和距离?
【发布时间】:2013-11-24 09:49:34
【问题描述】:

我想获取两个地理点坐标之间的道路距离。目前我正在获取路径,但它不是很准确,因为它考虑了道路的起点和终点,而不是道路中的曲线。我正在使用以下代码:

Double sLat = Double.parseDouble(getIntent().getStringExtra("sLat"));
Double sLon = Double.parseDouble(getIntent().getStringExtra("sLon"));
Double dLat = Double.parseDouble(getIntent().getStringExtra("dLat"));
Double dLon = Double.parseDouble(getIntent().getStringExtra("dLon"));

MapView mv = (MapView) findViewById(R.id.mapview);
mv.setBuiltInZoomControls(true);
MapController mc = mv.getController();
ArrayList<GeoPoint> all_geo_points = getDirections(sLat, sLon, dLat, dLon);
GeoPoint moveTo = (GeoPoint) all_geo_points.get(0);
mc.animateTo(moveTo);
mc.setZoom(18);
mv.getOverlays().add(new MyOverlay(all_geo_points));

    public static ArrayList<GeoPoint> getDirections(double lat1, double lon1, double lat2, double lon2) 
{
    String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1  + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
    String tag[] = { "lat", "lng" };
    ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>();
    HttpResponse response = null;
    try 
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        if (doc != null) {
            NodeList nl1, nl2;
            nl1 = doc.getElementsByTagName(tag[0]);
            nl2 = doc.getElementsByTagName(tag[1]);
            if (nl1.getLength() > 0) {
                list_of_geopoints = new ArrayList<GeoPoint>();
                for (int i = 0; i < nl1.getLength(); i++) {
                     Node node1 = nl1.item(i);
                     Node node2 = nl2.item(i);
                     double lat = Double.parseDouble(node1.getTextContent());
                     double lng = Double.parseDouble(node2.getTextContent());
                     list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)));
                 }
             } else {
                        // No points found
                    }
    }
} 

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

            return list_of_geopoints;
        }

        @Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }

谁能给我一些想法如何改进这条路线以及如何通过公路获得距离。目前我正在以公里为单位获得乌鸦飞行的距离,但我需要通过公路获得距离。

提前致谢。

【问题讨论】:

    标签: android google-maps latitude-longitude driving-directions geopoints


    【解决方案1】:

    【讨论】:

    • 谢谢。你的回答帮助我拉开了距离
    【解决方案2】:

    如果你想得到所有的分数,你必须解析&lt;polyline&gt;&lt;points&gt;a~l~Fjk~uOwHJy@P&lt;/points&gt;&lt;/polyline&gt;,而不是只取&lt;lat&gt;&lt;lng&gt;

    Android Maps Utils 将帮助您解析那个有趣的字符串。

    【讨论】:

    • 我不明白这一点。如何解析 a~l~Fjk~uOwHJy@P
    • 好吧,我找到了一些解析点的代码,但即便如此我也遇到了问题,因为它不是完整的路径。它在目的地前几米处结束。有什么想法吗?
    • @Guria 整个xml文件中有很多这样的元素,所以把它们全部解析并连接在一起。
    【解决方案3】:
        <html>
        <head><title>dis</title></head>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?
        sensor=false&libraries=geometry"></script>
    
        <script>
        var p1 = new google.maps.LatLng(45.463688, 9.18814);
        var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);
    
        alert(calcDistance(p1, p2));
    
        //calculates distance between two points in km's
        function calcDistance(p1, p2) {
          return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
        }
    
        </script>
        <body>
         </body>
         </html>
    

    【讨论】:

    • 这是基本结构,您可以稍后将输入的坐标从静态变为动态,此方法经过位移测试并以公里为单位给出答案。
    猜你喜欢
    • 2012-08-06
    • 2019-08-05
    • 1970-01-01
    • 2019-07-26
    • 2011-02-27
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多