【问题标题】:How to plot the marker in the middle point of the polyline in Google Maps?如何在谷歌地图的折线中点绘制标记?
【发布时间】:2017-05-13 10:24:49
【问题描述】:

我在 Google 地图中绘制了一条折线,当单击折线时,我希望在折线上有一个标记。那么只有标记应该出现在折线的中间。

@Override
public void onPolylineClick(Polyline polyline) {
    double latitude = polyline.getPoints().get(0).latitude;
    double longitude = polyline.getPoints().get(0).longitude;
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(new LatLng(latitude, longitude))
        .zoom(18).build();
}

标记上方的代码仅在第一点绘制,但我需要它在折线的中间。我该怎么做?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    你可以通过获取边界的中心来找到折线的中心。

    我还没有执行代码,但我希望它能正常工作。

    public LatLng getPolylineCentroid(Polyline p) {
    
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for(int i = 0; i < p.getPoints().size(); i++){
            builder.include(p.getPoints().get(i));
        }
    
        LatLngBounds bounds = builder.build();
    
        return bounds.getCenter();
    }
    

    【讨论】:

    • 这个方法假设地球是平的。这不是测地线。
    • 这项工作如果折线是直线,我们应该使用extrapolate方法得到作为距离发送的latlan
    【解决方案2】:

    您可以使用我在此解决方案中提出的extrapolate 函数:How can I extrapolate farther along a road?

    double middleDistance = SphericalUtil.computeLength(polyline.getPoints());
    
    LatLng markerPosition = extrapolate(polyline.getPoints(), polyline.getPoints().get(0), middleDistance);
    
    mMap.addMarker(new MarkerOptions().position(markerPosition).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(markerPosition)
        .zoom(18).build();
    

    【讨论】:

    • 当我尝试这个时,extrapolate 方法使用 !PolyUtil.isLocationOnPath 返回 null,考虑到输入,这对我来说没有意义。
    猜你喜欢
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2016-05-14
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多