如何在谷歌地图上显示多边形所有边的距离

问题描述 投票:0回答:1

我想显示在谷歌地图上制作的多边形所有边的长度。也许可以在多边形的中心打印多边形的面积

这是显示多边形长度的代码,但我似乎无法获得距第一个点和最后一个点的距离

private void polygonLabels() {
        int startIndex = polygonMarkers.size();

        for (int i = startIndex + 1; i < polygonPoints.size(); i++) {
            LatLng startPoint = polygonPoints.get(i - 1);
            LatLng endPoint = polygonPoints.get(i % polygonPoints.size());

            double distance = SphericalUtil.computeDistanceBetween(startPoint, endPoint);
            int distanceInt = (int) distance;
            String distanceString = distanceInt + "m";

            LatLng midpoint = SphericalUtil.interpolate(startPoint, endPoint, 0.5);

            if (i < polygonMarkers.size()) {
                Marker marker = polygonMarkers.get(i);
                TextView textView = (TextView) marker.getTag();
                textView.setText(distanceString);
            } else {
                Marker marker = addText(getApplicationContext(), mMap, midpoint, distanceString, 2, 18);
                polygonMarkers.add(marker);
            }
        }
    }
  public Marker addText(final Context context, final GoogleMap map, final LatLng location, final String text, final int padding, final int fontSize) {
        Marker marker = null;

        if (context == null || map == null || location == null || text == null
                || fontSize <= 0) {
            return marker;
        }

        final TextView textView = new TextView(context);
        textView.setText(text);
        textView.setTextSize(fontSize);

        final Paint paintText = textView.getPaint();

        final Rect boundsText = new Rect();
        paintText.getTextBounds(text, 0, textView.length(), boundsText);
        paintText.setTextAlign(Paint.Align.CENTER);

        final Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        final Bitmap bmpText = Bitmap.createBitmap(boundsText.width() + 2 * padding,
                boundsText.height() + 2 * padding, conf);

        final Canvas canvasText = new Canvas(bmpText);
        paintText.setColor(Color.BLACK);

        canvasText.drawText(text, bmpText.getWidth() / 2f, bmpText.getHeight() / 2f - boundsText.exactCenterY(), paintText);


        MarkerOptions markerOptions = new MarkerOptions()
                .position(location)
                .icon(BitmapDescriptorFactory.fromBitmap(bmpText))
                .anchor(0.5f, 0.5f);

        marker = map.addMarker(markerOptions);

        marker.setTag(textView);

        return marker;
    }

Image of what this code displays

java android google-maps
1个回答
0
投票

替换第三行

i < polygonPoints.size();

i <= polygonPoints.size();

此外,如果你在第2行设置了

startIndex = polygonMarkers.size()
,那么在for循环中条件
if (i < polygonMarkers.size())
always false,你可以忽略这种情况。

如果你想计算多边形的面积,你必须实现鞋带公式,请参阅Wikipedia(它与那里的三角形公式相同)。

© www.soinside.com 2019 - 2024. All rights reserved.