我已经实现了google_maps_fltuter
,并且每当用户单击带边框的矩形内任意位置的其他路线时,我都需要使用新坐标更新地图上的路线(请参见下图) 。最初呈现视图时,地图会安装第一条路线的坐标,但我还需要能够显示不同的路线。
我有一个GestureDetector
小部件允许发生这种情况,但是无论如何它都不会触发RouteMap
类。
class _PlanJourney extends State<PlanJourney> {
...
GestureDetector(
onTap: () => RouteMap(from, to, routeCoordinates, i),
child: Container(...)
)
...
}
这是一个根据传递给该类的坐标创建地图的类。
class RouteMap extends StatefulWidget {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
@override
_RouteMap createState() => _RouteMap(fromCoordinate, toCoordinate, routeCoordinates, routeIndex);
}
class _RouteMap extends State<RouteMap> {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
_RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
Completer<GoogleMapController> _controller = Completer();
GoogleMapController mapController;
LatLng _setCenter() {
return LatLng(double.parse(this.toCoordinate.split(',').toList()[1]),
double.parse(this.toCoordinate.split(',').toList()[0]));
}
final Set<Marker> _markers = {};
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
void _onMapCreated(GoogleMapController controller) {
_setCenter();
mapController = controller;
_controller.complete(controller);
// the coordinates coming from the API are in LonLat format
// Google map only accepts LatLon format so we have to swap the around
LatLng latLng_1 = LatLng(double.parse(this.fromCoordinate.split(',').toList()[1]),
double.parse(this.fromCoordinate.split(',').toList()[0]));
LatLng latLng_2 = LatLng(double.parse(this.toCoordinate.split(',').toList()[1]),
double.parse(this.toCoordinate.split(',').toList()[0]));
setState(() {
_markers.clear();
addMarker(latLng_1, "Title", "description");
addMarker(latLng_2, "Title", "description");
});
_add(routeIndex);
}
...
更新
我稍微更改了代码,但问题仍然存在。 SetState
更新了我需要发送给RouteMap
类的值,但该类本身不会更新。
class _PlanJourney extends State<PlanJourney> {
...
GestureDetector(
onTap: () {
setState(() {
routeIndex = i; // this is the value I need to send to RouteMap class
// print(routeIndex) -> the routeIndex changes
});
},
child: Container(...) // *note the map is not in this container
)
...
}
...
// the map is initially created here
Container(
margin: EdgeInsets.only(bottom: 10),
height: 400,
child: RouteMap(widget.from, widget.to, routeCoordinates[routeIndex], journeyData['routes'][routeIndex])
)
...
...
class RouteMap extends StatefulWidget {
final fromCoordinate;
final toCoordinate;
final routeCoordinates;
final routeIndex;
RouteMap(this.fromCoordinate, this.toCoordinate, this.routeCoordinates, this.routeIndex);
@override
_RouteMap createState() => _RouteMap();
}
class _RouteMap extends State<RouteMap> {
Completer<GoogleMapController> _controller = Completer();
GoogleMapController mapController;
final Set<Marker> _markers = {};
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
...
}
...
答案 0 :(得分:0)
在onTap()中,您可以更新选定的值,如下所示:
GestureDetector(
onTap: () => setState(() {
selectedFrom = from;
selectedTo = to;
selectedRuteCoordinates = routeCoordinates;
}),
child: Container(
child: RouteMap(from, to, routeCoordinates, i),
)
)