Untitled

 avatar
unknown
plain_text
2 years ago
3.6 kB
3
Indexable
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _HomePageState();
}

class _HomePageState extends State<MyApp> {
// created controller to display Google Maps
  Completer<GoogleMapController> _controller = Completer();
//on below line we have set the camera position
  static final CameraPosition _kGoogle = const CameraPosition(
    target: LatLng(19.0759837, 72.8776559),
    zoom: 4,
  );

  final List<Marker> _markers = [];
  final Set<Polyline> _polyline = {};

// list of locations to display polylines
  List<LatLng> latLen = [
    LatLng(19.0759837, 72.8776559),
    LatLng(28.679079, 77.069710),
  ];
  @override
  void initState() {
    super.initState();

    // declared for loop for various locations
    for (int i = 0; i < latLen.length; i++) {
      _markers.add(
        Marker(
          markerId: MarkerId(i.toString()),
          position: latLen[i],
          icon: BitmapDescriptor.defaultMarker,
        ),
      );
      setState(() {});
      _polyline.add(Polyline(
        polylineId: PolylineId('1'),
        points: latLen,
        color: Colors.green,
      ));
    }
  }

  updateMarker(id) {
    final marker =
        _markers.firstWhere((element) => element.markerId.value == "0");

    Marker _marker = Marker(
      markerId: marker.markerId,
      onTap: () {
        debugPrint("Marker Tapped");
      },
      position: LatLng(marker.position.latitude, marker.position.longitude),
      icon: marker.icon,
      infoWindow: InfoWindow(title: 'my new Title'),
    );

    setState(() {
      _markers[id] = _marker;
    });
  }

  addMarker(LatLng latLng, int i) {
    _markers.add(Marker(
      markerId: MarkerId("currentLocation$i"),
      icon: BitmapDescriptor.defaultMarker,
      position: latLng,
    ));

    setState(() {});
  }

  List<LatLng> latLenss = [
    LatLng(26.850000, 80.949997),
    LatLng(24.879999, 74.629997),
    LatLng(16.166700, 74.833298),
    LatLng(12.971599, 77.594563),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          heroTag: 'uniqueTag',
          onPressed: () async {
            // updateMarker(0);

            for (int i = 0; i < latLenss.length; i++) {
              if (i == 0) {
                Timer.periodic(Duration(seconds: 3),
                    (Timer t) => addMarker(latLenss[i], i));
              }
              if (i == 1) {
                Timer.periodic(Duration(seconds: 4),
                    (Timer t) => addMarker(latLenss[i], i));
              }
              if (i == 2) {
                Timer.periodic(Duration(seconds: 5),
                    (Timer t) => addMarker(latLenss[i], i));
              }
              if (i == 3) {
                Timer.periodic(Duration(seconds: 6),
                    (Timer t) => addMarker(latLenss[i], i));
              }
            }
          },
        ),
        body: SafeArea(
          child: GoogleMap(
            initialCameraPosition: _kGoogle,
            markers: Set<Marker>.of(_markers),
            polylines: _polyline,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
          ),
        ),
      ),
    );
  }
}
Editor is loading...