我正试图使用地理编码器从地址字符串中获取坐标,并使用该地址的LatLng设置初始相机位置。但谷歌地图显示蓝色屏幕。放大后我发现那是大海。如何使用startPoint字符串将其设置到所需位置?
String startPoint = '1 Ash Park, Pembroke Dock, SA72';
@override
void initState() {
initialize();
super.initState();
}
Future<void> initialize() async {
locationVM = Provider.of<LocationViewModel>(context, listen: false);
await locationVM.getCoordinates(startPoint, endPoint);
locationVM.addMarker(
LatLng(locationVM.coordinatesStart!.latitude!,
locationVM.coordinatesStart!.longitude!),
"origin",
BitmapDescriptor.defaultMarker);
locationVM.addMarker(
LatLng(locationVM.coordinatesEnd!.latitude!,
locationVM.coordinatesEnd!.longitude!),
"destination",
BitmapDescriptor.defaultMarkerWithHue(90));
await locationVM.getPolyLines();
}
ViewModel getCoordinates函数
Future<void> getCoordinates(String addressStart,String addressEnd) async {
LocationService locationService = LocationService();
coordinatesStart = (await locationService.getCoordinates(addressStart))!;
coordinatesEnd = (await locationService.getCoordinates(addressEnd))!;
notifyListeners();
}
LocationService getCoordinates函数
Future<Coordinates?> getCoordinates (String address) async {
var addresses = await Geocoder.local.findAddressesFromQuery(address);
var first = addresses.first;
print("Adddress ${first.featureName} : Coordinates ${first.coordinates}");
return first.coordinates;
}
GoogleMap(
onMapCreated: (controller) {
setState(() {
mapController = controller;
});
},
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: LatLng(
locationVM.coordinatesStart!.latitude!,
locationVM.coordinatesStart!.longitude!,
),
zoom: 10,
),
markers: Set<Marker>.of(locationVM.markers.values),
polylines: Set<Polyline>.of(locationVM.polylines.values),
),
它在控制台中抛出的异常如下:
Unsupported class loader
E/flutter ( 8235): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(failed, Failed, null, null)
E/flutter ( 8235): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7)
E/flutter ( 8235): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18)
E/flutter ( 8235): <asynchronous suspension>
E/flutter ( 8235): #2 LocalGeocoding.findAddressesFromQuery (package:flutter_geocoder/services/local.dart:18:28)
E/flutter ( 8235): <asynchronous suspension>
E/flutter ( 8235): #3 LocationService.getCoordinates (package:goochil_driver_app/services/location_service.dart:9:21)
E/flutter ( 8235): <asynchronous suspension>
E/flutter ( 8235): #4 LocationViewModel.getCoordinates (package:goochil_driver_app/viewModels/location_view_model.dart:20:25)
E/flutter ( 8235): <asynchronous suspension>
E/flutter ( 8235): #5 _TripDetailsState.initialize (package:goochil_driver_app/views/trip_details.dart:41:5)
E/flutter ( 8235): <asynchronous suspension>
E/flutter ( 8235):
W/goochil.taxiap( 8235): Unsupported class loader
-
很可能您没有在initialize((方法中获得坐标。尝试打印出initialize方法中引入的坐标。
-
我对此不太确定,但在initialize((方法中,您使用的providerlisten设置为false。因此,从逻辑上讲,位置VM.coordinateStart不会有更新的值,因为它没有听取变化。解决此问题的方法是在小部件构建方法中放置另一个提供程序变量。见下文。。。。。
@override
Widget build(BuildContext context) {
var locationProvider = Provider.of<LocationViewModel>(context);
return Scaffold(
body: GoogleMap(
onMapCreated: (controller) {
setState(() {
mapController = controller;
});
},
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: LatLng(
locationProvider.coordinatesStart!.latitude!,
locationProvider.coordinatesStart!.longitude!,
),
zoom: 10,
),
markers: Set<Marker>.of(locationProvider.markers.values),
polylines: Set<Polyline>.of(locationProvider.polylines.values),
),
);
}
所以这是视图模型初始化的问题。我做了以下更改
@override
void initState() {
Provider.of<LocationViewModel>(context,listen: false).getData();
super.initState();
}
在构建方法中,我像这样访问
@override
Widget build(BuildContext context) {
var locationVM = Provider.of<LocationViewModel>(context);
ViewModel获取的数据看起来像这个
Future<void> getData() async {
await this.getCoordinates();
this.addMarker(LatLng(coordinatesStart!.latitude!, coordinatesStart!.longitude!), 'origin', BitmapDescriptor.defaultMarker);
await this.getPolyLines();
notifyListeners();
}
但是摄像机的位置仍然没有被初始化为正确的坐标。