我正在尝试实现一个显示方向和实时位置的地图。我已经获得了 flutter 项目的 API,并在我需要的所有应用程序(iOS、Android 和 Web)中指定了它。 然后我导入了实现它所需的必要包。地图正在显示,但折线点不是,它一直给我这个错误。
错误:XMLHttpRequest 错误。
这是地图的有状态小部件。
class MapSampleState extends State<MapSample> {
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
@override
void initState() {
// getCurrentLocation();
getPolyPoints();
super.initState();
}
static const LatLng sourceLocation =
LatLng(37.42796133580664, -122.085749655962);
static const LatLng destination =
LatLng(37.43296265331129, -122.08832357078792);
List<LatLng> polylineCoordinates = [];
@override
Widget build(BuildContext context) {
return GoogleMap(
initialCameraPosition: CameraPosition(
target: sourceLocation,
zoom: 14.4746,
),
markers: {
Marker(
markerId: MarkerId('source'),
position: sourceLocation
),
Marker(
markerId: MarkerId('destination'),
position: destination
),
},
polylines: {
Polyline(
polylineId: const PolylineId('route'),
points: polylineCoordinates,
color: signUpPageBackgroundColor,
)
},
);
}
void getPolyPoints() async {
PolylinePoints polylinePoints = PolylinePoints();
PolylineResult result = await polylinePoints
.getRouteBetweenCoordinates(
googleApiKey,
PointLatLng(sourceLocation.latitude, sourceLocation.longitude),
PointLatLng(destination.latitude, destination.longitude),
);
if(result.points.isNotEmpty){
result.points
.forEach((PointLatLng point)
=> polylineCoordinates.add(
LatLng(point.latitude, point.longitude))
);
setState(() {});
}else{
print(result.errorMessage);
}
print(result.points);
}
}
我尝试限制 google_api,认为这就是问题所在,但它仍然不起作用。最初地图没有显示,因为我使用的是设备预览包,但我下载了 google_map_web 并且它可以工作,但是 polyline_points 支持网络版本的地图,但没有显示。
有一个开放的 PR,可以让人们使用代理服务器。同时尝试一下叉子。这可能会解决您的问题。
https://github.com/Dammyololade/flutter_polyline_points/pull/91