如何解决如何解决map flutter中不稳定的折线
我想在地图上绘制位置距离。然后当用户移动定位销时也会移动它。我已参考此 page。我面临的问题是折线有些时候出来了,有些时候没有出来。我使用的包是 google_maps_Flutter: ^2.0.3、lutter_polyline_points: ^0.2.6 和 location: ^4.1.1。任何人都可以帮助我吗?提前致谢。这是我的代码:
const double CAMERA_ZOOM = 18;
const double CAMERA_TILT = 80;
const double CAMERA_bearing = 30;
const LatLng SOURCE_LOCATION = LatLng(2.3896,122.2501);
const LatLng DEST_LOCATION = LatLng(-2.2855,122.2625);
class MapPage extends StatefulWidget {
@override
_MapPageState createState() => _MapPageState();
}
class _MapPageState extends State<MapPage> {
Completer<GoogleMapController> _controller = Completer();
Set<Marker> _markers = Set<Marker>();
Set<polyline> _polylines = Set<polyline>();
List<LatLng> polylineCoordinates = [];
polylinePoints polylinePoints;
String googleAPIKey = 'MY Google Api KEY';
BitmapDescriptor sourceIcon;
BitmapDescriptor destinationIcon;
LocationData currentLocation;
LocationData destinationLocation;
Location location;
@override
void initState() {
super.initState();
location = new Location();
polylinePoints = polylinePoints();
location.onLocationChanged.listen((LocationData cLoc) {
currentLocation = cLoc;
updatePinOnMap();
});
setSourceAndDestinationIcons();
setinitialLocation();
}
void setSourceAndDestinationIcons() async {
sourceIcon = await BitmapDescriptor.fromAssetimage(
ImageConfiguration(devicePixelRatio: 2.5),'assets/driving_pin.png');
destinationIcon = await BitmapDescriptor.fromAssetimage(
ImageConfiguration(devicePixelRatio: 2.5),'assets/destination_map_marker.png');
}
void setinitialLocation() async {
currentLocation = await location.getLocation();
destinationLocation = LocationData.fromMap({
"latitude": DEST_LOCATION.latitude,"longitude": DEST_LOCATION.longitude,});
}
@override
Widget build(BuildContext context) {
CameraPosition initialCameraPosition = CameraPosition(
zoom: CAMERA_ZOOM,tilt: CAMERA_TILT,bearing: CAMERA_bearing,target: SOURCE_LOCATION);
if (currentLocation != null) {
initialCameraPosition = CameraPosition(
target: LatLng(currentLocation.latitude,currentLocation.longitude),zoom: CAMERA_ZOOM,bearing: CAMERA_bearing);
}
return Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition: initialCameraPosition,myLocationEnabled: true,compassEnabled: true,tiltGesturesEnabled: false,markers: _markers,polylines: _polylines,mapType: MapType.normal,onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
showPinsOnMap();
},)
],),);
}
void showPinsOnMap() {
//get a LatLng for the source location from the LocationData currentLocation object
var pinPosition =
LatLng(currentLocation.latitude,currentLocation.longitude);
var destPosition =
LatLng(destinationLocation.latitude,destinationLocation.longitude);
_markers.add(Marker(
markerId: MarkerId('sourcePin'),position: pinPosition,icon: sourceIcon));
_markers.add(Marker(
markerId: MarkerId('destPin'),position: destPosition,icon: destinationIcon));
setpolylines();
}
void setpolylines() async {
polylineResult result = await polylinePoints.getRouteBetweenCoordinates(
googleAPIKey,PointLatLng(currentLocation.latitude,PointLatLng(
destinationLocation.latitude,destinationLocation.longitude));
if (result.points.isNotEmpty) {
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude,point.longitude));
});
setState(() {
_polylines.add(polyline(
width: 5,polylineId: polylineId("poly"),color: Color.fromARGB(255,40,122,198),points: polylineCoordinates));
});
}
}
void updatePinOnMap() async {
CameraPosition cameraPosition = CameraPosition(
zoom: CAMERA_ZOOM,target: LatLng(currentLocation.latitude,);
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
setState(() {
var pinPosition =
LatLng(currentLocation.latitude,currentLocation.longitude);
_markers.add(Marker(
markerId: MarkerId('sourcePin'),icon: sourceIcon));
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。