我正在使用 Google 地图。但我遇到了两个问题。
setState(() {});
分配值时,它不起作用。这是我的代码:
class MerchantMap extends StatefulWidget {
final data;
const MerchantMap({Key key, this.data}) : super(key: key);
@override
_MerchantMapState createState() => _MerchantMapState();
}
class _MerchantMapState extends State<MerchantMap> {
Completer<GoogleMapController> _controller = Completer();
Location _location = Location();
static LatLng _currentPosition = LatLng(90.0, 90.0);
String merchantName = "";
String merchantPhone = "";
List<Marker> allMarkersList = [];
static CameraPosition _kGooglePlex = CameraPosition(
target: _currentPosition,
zoom: 14.0,
);
void _onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
_location.onLocationChanged;
}
Future _getUserLocation() async {
var position = await _location.getLocation();
setState(() {
_currentPosition = LatLng(position.latitude, position.longitude);
});
}
@override
void initState() {
super.initState();
_getUserLocation().then((value) {
setState(() {
_kGooglePlex = CameraPosition(
target: _currentPosition,
zoom: 14.0,
);
});
});
allMarkersList.addAll(widget.data.merchants.map<Marker>((list) {
return Marker(
markerId: MarkerId("${list["merchant_id"]}"),
draggable: false,
onTap: () {
print("Taped On Marker");
setState(() {});
},
position: LatLng(list["lat"], list["long"]),
infoWindow: InfoWindow(
title: "${list["merchant_name"]}",
snippet: "${list["mobile"]}",
onTap: () {
print("object");
}),
);
}).toList());
}
@override
Widget build(BuildContext context) {
double _width = MediaQuery.of(context).size.width;
double _height = MediaQuery.of(context).size.height;
return SingleChildScrollView(
child: Container(
height: _height,
width: _width,
child: Column(
children: [
Container(
padding: EdgeInsets.all(10.0),
height: _height * 0.2,
child: Align(
alignment: Alignment.topLeft,
child: Text(
widget.data.exchangerName,
style: TextStyle(fontSize: 18.0),
)),
),
Container(
height: _height * 0.5,
width: _width,
child: GoogleMap(
compassEnabled: true,
myLocationButtonEnabled: true,
scrollGesturesEnabled: true,
mapType: MapType.normal,
zoomGesturesEnabled: true,
initialCameraPosition: _kGooglePlex,
onMapCreated: _onMapCreated,
onCameraMove: (CameraPosition cameraPosition) {},
myLocationEnabled: true,
markers: Set.from(allMarkersList),
mapToolbarEnabled: true,
),
),
],
),
),
);
}
}
我的代码哪里出了问题?我该如何解决这个问题?