【问题标题】:Android Google Maps - How to divide Google Maps route in equal segments?Android Google Maps - 如何将 Google Maps 路线分成相等的部分?
【发布时间】:2016-08-29 07:41:59
【问题描述】:

在我的自定义 Google 地图中,我使用此 URL 在两个位置(例如浦那到孟买)之间绘制了一条路线

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

解析此响应,我得到源和目标之间的点数并将其存储在ArrayList

ArrayList<LocationModel> listAllPoints = getPoints();

这个ArrayList 包含大约 2600 个点。

我的问题是,我如何将这条路线分成 10 个相等的部分,即包括源和目的地在内的总共 11 个点。像这样

|一个 |乙| C | D | E | F |克| H |我 | J |

在哪里 | = 每个点,字符 = 段

A和B、B和C、C和D的距离应该相等。

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

任何帮助将不胜感激。

【问题讨论】:

标签: java android google-maps


【解决方案1】:

参考这个简单的代码。

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

【讨论】:

  • 这段代码只是在地图上画了一条线。我已经做到了。如何将这条路线分成相等的部分?
猜你喜欢
  • 2018-07-02
  • 2016-05-19
  • 1970-01-01
  • 2013-02-12
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多