Reputation: 827
I'm beginner. I have small problem on my project. My project simply draw maker(s) on the google map.
This is a part of code to draw the map
child: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: const CameraPosition(
target: LatLng(-33.852, 151.211),
zoom: 2.0,
),
markers: Set<Marker>.of(markers.values),
// __addmarker(),
),
I create a void _addmarker()
for and a button to add marker. Like this
//void _addmarker()
Marker marker = Marker(
markerId: markerId,
position: LatLng(
11.052992, 106.681612,
),
Code of Button
FlatButton(
child: const Text('add'),
onPressed: _addmarker,
),
It's mean when I onPressed
, the map will draw marker with Lat,Lng on void _addmarker()
It's OK but now I want draw marker on the map but no need press button. How can I do that?
Upvotes: 2
Views: 6380
Reputation: 57
You can create Set of markers function like this:
Future<Set<Marker>> myMarkers() async {
List<Marker> mMarkers = [];
mMarkers.add(
Marker(
markerId: MarkerId('marker1'),
position: LatLng(15.392567, 44.278188),
),
);
mMarkers.add(
Marker(
markerId: MarkerId('marker1'),
position: LatLng(15.392029, 44.278113),
),
);
return mMarkers.toSet();
}
and you can use this function in FutureBuilder
widget like this:
FutureBuilder(
future: myMarkers(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
markers: snapshot.data,
);
},
),
```
Upvotes: 0
Reputation: 5345
So now you are adding your markers with a button and function and you want to add them without pressing a button?
You can create Set of markers on your state widget.
Set<Marker> markers = Set();
And then you can fill that Set of markers in your build method,
markers.addAll([
Marker(
markerId: MarkerId('value'),
position: LatLng(37.416780, -122.077430)),
Marker(
markerId: MarkerId('value2'),
position: LatLng(37.416000, -122.077000)),
]);
There is also various constructor methods for Set class. You can just use one, you don't have to add them. Set class And then you can pass that filled set to your Google Maps widget.
child: GoogleMap(
onMapCreated: _onMapCreated,
myLocationEnabled: true,
initialCameraPosition:
CameraPosition(target: LatLng(0.0, 0.0)),
markers: markers,
))),
Upvotes: 4