Android Google地图 - 如何在相同的细分中划分Google地图路线?

时间:2016-05-04 03:47:41

标签: java android google-maps

在我自定义的Google地图中,我使用此网址在两个地点(比如Pune到Mumbai)之间绘制路线

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 |我| J |

其中| =每个点,Character = segment

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

我应该可以通过在11个部分中分割listAllPoints来做到这一点,但问题是,listAllPoints内的点不是均匀分散的。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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));
                }
                // Adding route on the map
                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("Point A");
                mGoogleMap.addMarker(markerOptions);