折线不会在我的群集Google地图标记上绘制线条

时间:2018-02-04 10:21:33

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

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

首先,地图上有3个标记,所以我尝试用折线加入它们并拒绝了,对我来说代码似乎很好,但折线不可见,我该如何改进我的代码。

以下是我的代码:

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


        }

    }

1 个答案:

答案 0 :(得分:0)

您为每个点创建一个Polyline。您需要创建某种iterable(例如ArrayList),将LatLng添加到其中,然后将所有点添加到新的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);