【问题标题】:Polyline doesn't draw a line on my clustered Google map markers折线没有在我的集群 Google 地图标记上画线
【发布时间】:2018-07-14 09:25:58
【问题描述】:

我有 3 个点想要绘制 折线,但折线不可见。

首先,地图上有 3 个标记,所以我尝试用 polyline 将它们加入,但它拒绝了,对我来说代码看起来不错,但折线不可见, 如何改进我的代码。

下面是我的代码:

 @Override
    public void onMapReady(GoogleMap googleMap) {



        mMap = googleMap;

        setUpClusterer(mMap);
    }



    //clustered map

    private void setUpClusterer(GoogleMap mMap) {
        // Position the map.

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-23.684, 133.903), 4));
        // Initialize the manager with the context and the map.
        // (Activity extends context, so we can pass 'this' in the constructor.)
        mClusterManager = new ClusterManager<MyItem>(this, mMap);

        // Point the map's listeners at the listeners implemented by the cluster
        // manager.
        mMap.setOnCameraIdleListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);


        // Add cluster items (markers) to the cluster manager.
        addItems(mMap);
    }

    private void addItems(GoogleMap mMap) {

        // Set some lat/lng coordinates to start with.
        double lat = 51.5145160;
        double lng = -0.1270060;
        HashMap<Double,Double> hm = new HashMap<>();

       /* new LatLng(-34.747, 145.592),
                new LatLng(-34.364, 147.891),
                new LatLng(-33.501, 150.217),*/
        hm.put(-34.747,145.592);
        hm.put(-34.364,147.891);
        hm.put(-33.501,150.217);

        // Add ten cluster items in close proximity, for purposes of this example.

        for(Map.Entry m:hm.entrySet()){
            System.out.println(m.getKey()+" "+m.getValue());
            MyItem offsetItem = new MyItem(Double.parseDouble(m.getKey().toString()),Double.parseDouble(m.getValue().toString()));
            mClusterManager.addItem(offsetItem);

            Polyline polyline = mMap.addPolyline(new PolylineOptions()
                    .clickable(true)
                    .add(
                            new LatLng(Double.parseDouble(m.getKey().toString()), Double.parseDouble(m.getValue().toString()))

                    ));
            stylePolyline(polyline);


        }

    }

【问题讨论】:

    标签: java android google-maps google-maps-markers google-polyline


    【解决方案1】:

    您正在为每个点创建一个Polyline。您需要创建某种iterable(例如ArrayList),将LatLngs 添加到其中,然后将所有点添加到新的Polyline

    List<LatLngs> latLngs = new ArrayList<LatLng>();
    
    for(Map.Entry m:hm.entrySet()){
        MyItem offsetItem = new MyItem(Double.parseDouble(m.getKey().toString()),Double.parseDouble(m.getValue().toString()));
        mClusterManager.addItem(offsetItem);
    
        latLngs.add(new LatLng(Double.parseDouble(m.getKey().toString()), Double.parseDouble(m.getValue().toString())));
    }
    
    Polyline polyline = mMap.addPolyline(new PolylineOptions()
                                .clickable(true)
                                .addAll(latLngs));
    stylePolyline(polyline);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-13
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 2011-03-06
      • 2020-08-17
      相关资源
      最近更新 更多