使用 Polyline 通过 google_maps_flutter 插件制作自定义路线 - Flutter
Using Polyline for making custom route with google_maps_flutter plugin - Flutter
google_maps_flutter
0.5.6 在 GoogleMap
上添加了对 Polylines
的支持。
但是在我看来没有可用的代码和文档。
存在一个名为 maps_view 的插件,它支持多段线
import 'package:map_view/map_view.dart';
import 'package:map_view/polyline.dart';
...
MapView mapView = MapView();
mapView.addPolyline(Polyline('my_polyline', [
Location(45.52309483308097, -122.67339684069155),
Location(45.52298442915803, -122.66339991241693),
]));
我需要 google_maps_flutter
内置的相同功能。
在您的 StatefulWidget
子类中试试这个。
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
void _add() {
final String polylineIdVal = 'polyline_id_$_polylineIdCounter';
_polylineIdCounter++;
final PolylineId polylineId = PolylineId(polylineIdVal);
final Polyline polyline = Polyline(
polylineId: polylineId,
consumeTapEvents: true,
color: Colors.red,
width: 5,
points: _createPoints(),
);
setState(() {
_mapPolylines[polylineId] = polyline;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Maps"),
actions: <Widget>[IconButton(icon: Icon(Icons.add), onPressed: _add)],
),
body: GoogleMap(
initialCameraPosition: const CameraPosition(target: LatLng(0, 0), zoom: 4.0),
polylines: Set<Polyline>.of(_mapPolylines.values),
),
);
}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
points.add(LatLng(1.875249, 0.845140));
points.add(LatLng(4.851221, 1.715736));
points.add(LatLng(8.196142, 2.094979));
points.add(LatLng(12.196142, 3.094979));
points.add(LatLng(16.196142, 4.094979));
points.add(LatLng(20.196142, 5.094979));
return points;
}
google_maps_flutter
0.5.6 在 GoogleMap
上添加了对 Polylines
的支持。
但是在我看来没有可用的代码和文档。
存在一个名为 maps_view 的插件,它支持多段线
import 'package:map_view/map_view.dart';
import 'package:map_view/polyline.dart';
...
MapView mapView = MapView();
mapView.addPolyline(Polyline('my_polyline', [
Location(45.52309483308097, -122.67339684069155),
Location(45.52298442915803, -122.66339991241693),
]));
我需要 google_maps_flutter
内置的相同功能。
在您的 StatefulWidget
子类中试试这个。
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
void _add() {
final String polylineIdVal = 'polyline_id_$_polylineIdCounter';
_polylineIdCounter++;
final PolylineId polylineId = PolylineId(polylineIdVal);
final Polyline polyline = Polyline(
polylineId: polylineId,
consumeTapEvents: true,
color: Colors.red,
width: 5,
points: _createPoints(),
);
setState(() {
_mapPolylines[polylineId] = polyline;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Maps"),
actions: <Widget>[IconButton(icon: Icon(Icons.add), onPressed: _add)],
),
body: GoogleMap(
initialCameraPosition: const CameraPosition(target: LatLng(0, 0), zoom: 4.0),
polylines: Set<Polyline>.of(_mapPolylines.values),
),
);
}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
points.add(LatLng(1.875249, 0.845140));
points.add(LatLng(4.851221, 1.715736));
points.add(LatLng(8.196142, 2.094979));
points.add(LatLng(12.196142, 3.094979));
points.add(LatLng(16.196142, 4.094979));
points.add(LatLng(20.196142, 5.094979));
return points;
}