问题描述
我有一个带有谷歌地图的应用程序,该应用程序获取我当前的位置,地图上有两个标记,标记的一个点和折线是从前一个屏幕传递的坐标。我希望折线的起点是我的当前位置。我还想使用我当前的位置作为第一个标记。如果折线和标记会随着驱动程序移动而改变位置,那就太好了。但这不是现在的优先事项。
到目前为止我所做的: 我能够获得我当前的位置。 我能够从硬编码坐标绘制折线到目标位置(目标位置由前一个屏幕提供)
我想做什么: 使用我当前的位置作为我的折线的起点 使用我的当前位置添加标记 驱动程序移动时更新折线和标记。
这是我目前的代码。
class _MyAppState extends State<RoutePage> {
late GoogleMapController mapController;
// Starting point latitude
// 5.6609595,-0.017155699999989338)
double _originLatitude = 5.555700;
// Starting point longitude
double _originLongitude = -0.196300;
// Destination latitude
double? destLatitude;
// Destination Longitude
double? destLongitude;
// Markers to show points on the map
Map<MarkerId,Marker> markers = {};
Map<polylineId,polyline> polylines = {};
List<LatLng> polylineCoordinates = [];
polylinePoints polylinePoints = polylinePoints();
Completer<GoogleMapController> _controller = Completer();
late Position currentPosition;
var geoLocator = Geolocator();
late Geolocator geolocatorTwo;
var locationoptions = Locationoptions(accuracy: LocationAccuracy.bestForNavigation,distanceFilter: 4);
Future<LatLng> getCurrentPositiion() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation);
currentPosition = position;
LatLng pos = LatLng(position.latitude,position.longitude);
mapController.animateCamera((CameraUpdate.newLatLng(pos)));
print('MyPosition');
print(currentPosition);
print(pos.longitude);
print(pos.latitude);
print(position.longitude);
print(position.latitude);
print(pos);
return pos;
}
static const LatLng _center = const LatLng(45.521563,-122.677433);
@override
void initState() {
/// add origin marker origin marker
_addMarker(
LatLng(_originLatitude,_originLongitude),"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();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: GoogleMap(
padding: EdgeInsets.only(top: 135),myLocationButtonEnabled: true,myLocationEnabled: true,mapType: MapType.normal,initialCameraPosition: googlePlex,onMapCreated: (GoogleMapController controller){
_controller.complete(controller);
mapController=controller;
getCurrentPositiion();
},polylines: Set<polyline>.of(polylines.values),markers: Set<Marker>.of(markers.values),),);
}
// This method will add markers to the map based on the LatLng position
_addMarker(LatLng position,String id,BitmapDescriptor descriptor) {
MarkerId markerId = MarkerId(id);
Marker marker =
Marker(markerId: markerId,icon: descriptor,position: position);
markers[markerId] = marker;
}
_addpolyLine(List<LatLng> polylineCoordinates) {
polylineId id = polylineId("poly");
polyline polyline = polyline(
polylineId: id,points: polylineCoordinates,width: 8,);
polylines[id] = polyline;
setState(() {});
}
void _getpolyline() async {
List<LatLng> polylineCoordinates = [];
print("In the polyline");
print(currentPosition);
print(currentPosition.longitude);
polylineResult result = await polylinePoints.getRouteBetweenCoordinates(
"APP KEY",PointLatLng(_originLatitude,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);
}
void getLocationUpdates(){
var hoMetabPositionStream = Geolocator.getPositionStream().listen((Position position) {
currentPosition = position;
LatLng pos = LatLng(position.latitude,position.longitude);
mapController.animateCamera(CameraUpdate.newLatLng(pos));
});
}
}
我错过了什么,为什么我不能只使用 currentPosition.latitude
和 currentPosition.longitude
?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)