我从昨天开始浏览它,找不到任何令人满意的解决方案。
我使用geolocator插件获取当前用户位置,并通过initState()方法对其进行了定义。
我在地图上设置了静态标记,但想在地图上获得动态标记。
我的代码:
Completer<GoogleMapController> _controller = Completer();
Position position;
Set<Marker> markers = Set();
List<Marker> addMarkers = [
Marker(
markerId: MarkerId("Quark city Id"),
infoWindow: InfoWindow(
title: "Quark city",
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
position: LatLng(30.706129, 76.692422)
),
Marker(
markerId: MarkerId("Godrej Company Id"),
infoWindow: InfoWindow(
title: "Godrej Company",
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
position: LatLng(30.701916, 76.695063)
),
Marker(
markerId: MarkerId("Radha Swami Chouwk Id"),
infoWindow: InfoWindow(
title: "Radha Swami Chouwk",
),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
position: LatLng(30.703092, 76.701069)
),
];
@override
void initState() {
markers.addAll(addMarkers); // add markers to the list
super.initState();
getCurrentLocation();
}
//Current user's marker:
void getCurrentLocation()async{
Position pos= await Geolocator().getCurrentPosition();
setState(() {
position = pos;
// added markers on user current location
markers.add(Marker(
markerId: MarkerId("current Id"),
infoWindow: InfoWindow(
title: "Current",
),
position: LatLng(position.latitude,position.longitude),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueOrange),
));
});
}
构建方法:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Google Map"), centerTitle: true,),
body:position == null
? Center(child: CircularProgressIndicator(),)
:GoogleMap(
......
......
markers: markers
)
);
}