簡體   English   中英

折線不會在群集的Google地圖標記上畫線

[英]Polyline doesn't draw a line on my clustered Google map markers

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


        }

    }

您正在為每個點創建一條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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM