使用LatLng的Arraylist绘制两点之间的多段线



我想用polylineoptions.addAll发送参数ArrayList<LatLng>在两点之间绘制多段线,但它没有显示结果。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    private GoogleMap mMap;
    private ArrayList<LatLng> arrayPoints = new ArrayList<>();
    PolylineOptions polylineOptions=new PolylineOptions();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        LatLng point=new LatLng(-35,151);
        LatLng point2=new LatLng(-40,151);
        polylineOptions.color(Color.RED);
        polylineOptions.width(3);
        arrayPoints.add(point);
        arrayPoints.add(point2);
        polylineOptions.addAll(arrayPoints);
    }
    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) 
    {
    }
}

看起来您没有将折线添加到地图

mMap.addPolyline(polylineOptions)

最新更新