flutter google maps marker code example

Example 1: flutter_map marker

class _MapDataState extends State<MapData> {
      List<SitesList> sl = [];
      List<SnoTelSite> sts = [];
      var marker = <Marker>[];

      _MapDataState(){
        print("map data state");
        loadSites().then((sitesdata) {
          print('Loaded Sites Asset JSON');
          //clone sitesdata into sts array
          sts..addAll(sitesdata);
          sts.forEach((s) {
            marker.add(
              Marker(
                point: new LatLng(double.parse(s.lat),double.parse(s.lng)),
                builder: (ctx) => _MarkerPopUp(sitename: s.name, siteelevation: s.elevation, siteid: s.siteID,),
              ),
            );
          });
        });
      }

      // @override
      // initState() {
      //   print("INIT STATE");
      //   super.initState();
      // }

      //local load assets ... constants hold path snotelsitesjson
      Future<String> _loadSiteAssets() async {
        return await rootBundle.loadString(snotelsitesjson);
      }

      Future loadSites() async {
        String jsonString = await _loadSiteAssets();
        final jsonResponse = json.decode(jsonString);
        SitesList sitesList = new SitesList.fromJson(jsonResponse);
        return sitesList.sites;
      }

    //main build and screen layout
      
      Widget build(BuildContext context) {
        print("BUILD LAYOUT");
        return MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.cyan,
          ),
          home: Scaffold(
              appBar: AppBar(
                backgroundColor: Colors.white,
                leading: Icon(Icons.ac_unit),
                title: Text("SnoTel Map"),
                actions: <Widget>[
                  IconButton(
                    icon: Icon(Icons.favorite),
                    onPressed: () {
                      debugPrint("Favorites");
                    },
                  ),
                  IconButton(
                    icon: Icon(Icons.feedback),
                    onPressed: () {
                      debugPrint("Message sent");
                    },
                  ),
                ],
              ),
              body: _MapWidget()),
        );
      }
    }

Example 2: gooogle maps flutter

dependencies:
  google_maps_flutter: ^0.5.28+1

Example 3: google maps for flutter

dependencies:
  google_maps_flutter: ^1.0.2

Example 4: flutter draw google map

import 'package:google_maps_flutter/google_maps_flutter.dart';

static const LatLng _center = const LatLng(33.738045, 73.084488);
final Set<Marker> _markers = {};
final Set<Polyline>_polyline={};

//add your lat and lng where you wants to draw polyline
LatLng _lastMapPosition = _center;
List<LatLng> latlng = List();
LatLng _new = LatLng(33.738045, 73.084488);
LatLng _news = LatLng(33.567997728, 72.635997456);

latlng.add(_new);
latlng.add(_news);

//call this method on button click that will draw a polyline and markers

void _onAddMarkerButtonPressed() {
    getDistanceTime();
    setState(() {
        _markers.add(Marker(
            // This marker id can be anything that uniquely identifies each marker.
            markerId: MarkerId(_lastMapPosition.toString()),
            //_lastMapPosition is any coordinate which should be your default 
            //position when map opens up
            position: _lastMapPosition,
            infoWindow: InfoWindow(
                title: 'Really cool place',
                snippet: '5 Star Rating',
            ),
            icon: BitmapDescriptor.defaultMarker,

        ));
        _polyline.add(Polyline(
            polylineId: PolylineId(_lastMapPosition.toString()),
            visible: true,
            //latlng is List<LatLng>
            points: latlng,
            color: Colors.blue,
        ));
    });

    //in your build widget method
    GoogleMap(
    //that needs a list<Polyline>
        polylines:_polyline,
        markers: _markers,
        onMapCreated: _onMapCreated,
        myLocationEnabled:true,
        onCameraMove: _onCameraMove,
        initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 11.0,
        ),

        mapType: MapType.normal,

    );
}

Tags:

Dart Example