add marker to google maps flutter 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
@override
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: set markers in google_map_flutter
List<Marker> _markers = []
_markers.add(
Marker(
markerId: MarkerId('SomeId'),
position: LatLng(38.123,35.123),
infoWindow: InfoWindow(
title: 'The title of the marker'
)
)
);
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(38.9647,35.2233),
zoom: 9.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
)