Android谷歌地图 - 如何将谷歌地图路线分成相等的段?

3

在我的自定义谷歌地图中,我使用以下URL在两个位置之间绘制路线(例如从浦那到孟买):

https://maps.googleapis.com/maps/api/directions/json?origin=Pune&destination=Mumbai&sensor=false&mode=driving&alternatives=false

enter image description here

解析此响应,获取源和目的地之间的点数,并将其存储在ArrayList中。
ArrayList<LocationModel> listAllPoints = getPoints();

这个 ArrayList 包含大约2600个点。

我的问题是,如何将这条路线分成10个等长的段,即总共11个点,包括起点和终点。像这样:

| A | B | C | D | E | F | G | H | I | J |

其中 | = 每个点,字符 = 段落

A和B之间的距离,B和C之间的距离,C和D之间的距离应该相等。

我应该能够通过将 listAllPoints 分成11个部分来实现这一点,但问题在于,listAllPoints 中的点并不均匀分布。

任何帮助都将不胜感激。


你想在地图上创建路径并添加多个途经点吗? - Ravi Vaghela
请查看这个链接,可能对你有帮助。http://stackoverflow.com/questions/919243/how-might-i-divide-a-google-maps-display-into-an-equal-number-of-parts - Badal Shah
1个回答

0

请参考这段简单的代码。

  1. 获取文档

                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";
    
    
        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    document = builder.parse(in);
    
  2. 绘制线条:

               ArrayList<LatLng> directionPoint = v2GetRouteDirection
                        .getDirection(document);
                PolylineOptions rectLine = new PolylineOptions().width(10)
                        .color(Color.RED);
    
                for (int i = 0; i < directionPoint.size(); i++) {
                    rectLine.add(directionPoint.get(i));
                }
                // 在地图上添加路线
                mGoogleMap.addPolyline(rectLine);
    
  3. 添加点

               MarkerOptions markerOptions = new MarkerOptions();
               Bitmap icon = BitmapFactory.decodeResource(
                        EMaps2.this.getResources(), R.drawable.pointa);
                    markerOptions
                            .icon(BitmapDescriptorFactory.fromBitmap(icon));
                markerOptions.position(fromPosition);
                markerOptions.title("A点");
                mGoogleMap.addMarker(markerOptions);
    

1
这段代码只是在地图上画一条线。我已经完成了。如何将该路线分割成相等的段? - Jay Donga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接