问题描述
我有一张地图,显示了我的当前位置和目的地位置。我能够获得我当前位置的纬度和经度。我可以通过输入纬度和经度来预先确定我的起始标记应该在哪里。我宁愿使用我当前位置的纬度和经度作为起点折线并作为我的制造商。
这是我的代码。
Completer<GoogleMapController> _controller = Completer();
late Position currentPosition;
var geoLocator = Geolocator();
late Geolocator geolocatorTwo;
var locationoptions = Locationoptions(accuracy: LocationAccuracy.bestForNavigation,distanceFilter: 4);
void getCurrentPositiion() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);
currentPosition = position;
LatLng pos = LatLng(position.latitude,position.longitude);
mapController.animateCamera((CameraUpdate.newLatLng(pos)));
}
在 initState 中,addMaker。
@override
void initState() {
double destLatitude = 5.6609595;
// Destination Longitude
double destLongitude = -0.0181556999;
/// add origin marker origin marker
_addMarker(
LatLng(currentPosition.latitude,currentPosition.longitude),"origin",BitmapDescriptor.defaultMarker,);
// Add destination marker
_addMarker(
LatLng(double.parse(widget.vendors!.latitude),double.parse(widget.vendors!.longitude)),"destination",BitmapDescriptor.defaultMarkerWithHue(90),);
_getpolyline();
super.initState();
}
void _getpolyline() async {
List<LatLng> polylineCoordinates = [];
polylineResult result = await polylinePoints.getRouteBetweenCoordinates(
"API KEY",PointLatLng(currentLocation.latitude,currentLocation.longitude),PointLatLng(double.parse(widget.vendors!.latitude),travelMode: TravelMode.driving,);
if (result.points.isNotEmpty) {
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude,point.longitude));
});
} else {
print(result.errorMessage);
}
_addpolyLine(polylineCoordinates);
}
我收到错误 currentLocation 尚未初始化。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)