颤振折线问题
Flutter polyline issue
我目前正在为 google 地图工作。我遵循 GCP 上的所有步骤,并为 google map.Now 启用方向 API 我想在源和目标之间的地图上显示多段线。但是当我 运行 一个在真实设备上的应用程序时,它无法显示。我只得到来源和目的地的标记。
请在下面查看我的代码。
import 'package:flutter/material.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
double _originLatitude = 6.5212402;
double _originLongitude = 3.3679965;
double _destLatitude = 6.849660;
double _destLongitude = 3.648190;
Map<MarkerId, Marker> markers = {};
PolylinePoints polylinePoints = PolylinePoints();
Map<PolylineId, Polyline> polylines = {};
class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(_originLatitude, _originLongitude),
zoom: 9.4746,
);
@override
void initState() {
_addMarker(
LatLng(_originLatitude, _originLongitude),
"origin",
BitmapDescriptor.defaultMarker,
);
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
BitmapDescriptor.defaultMarkerWithHue(90),
);
_getPolyline();
print("Enter at getpoly");
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Welcome to Flutter',
home: Scaffold(
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
myLocationEnabled: true,
tiltGesturesEnabled: true,
compassEnabled: true,
scrollGesturesEnabled: true,
zoomGesturesEnabled: true,
polylines: Set<Polyline>.of(polylines.values),
markers: Set<Marker>.of(markers.values),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
),
);
}
_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 = [];
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
"API_KEY", // My google API key
PointLatLng(_originLatitude, _originLongitude),
PointLatLng(_destLatitude, _destLongitude),
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);
}
}
如何用折线显示我的地图?
问题似乎出在您的 API 密钥上。我 运行 你的代码使用我自己的 API 键,我能够显示多段线(见下面的截图)。
请确保您的项目已link登录到有效的结算帐户,以便使用 Google Maps Platform API。请参阅此 link https://developers.google.com/maps/documentation/directions/quickstart#before_you_begin
的“设置项目”部分的第 2 步
我目前正在为 google 地图工作。我遵循 GCP 上的所有步骤,并为 google map.Now 启用方向 API 我想在源和目标之间的地图上显示多段线。但是当我 运行 一个在真实设备上的应用程序时,它无法显示。我只得到来源和目的地的标记。 请在下面查看我的代码。
import 'package:flutter/material.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
double _originLatitude = 6.5212402;
double _originLongitude = 3.3679965;
double _destLatitude = 6.849660;
double _destLongitude = 3.648190;
Map<MarkerId, Marker> markers = {};
PolylinePoints polylinePoints = PolylinePoints();
Map<PolylineId, Polyline> polylines = {};
class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(_originLatitude, _originLongitude),
zoom: 9.4746,
);
@override
void initState() {
_addMarker(
LatLng(_originLatitude, _originLongitude),
"origin",
BitmapDescriptor.defaultMarker,
);
_addMarker(
LatLng(_destLatitude, _destLongitude),
"destination",
BitmapDescriptor.defaultMarkerWithHue(90),
);
_getPolyline();
print("Enter at getpoly");
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Welcome to Flutter',
home: Scaffold(
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kGooglePlex,
myLocationEnabled: true,
tiltGesturesEnabled: true,
compassEnabled: true,
scrollGesturesEnabled: true,
zoomGesturesEnabled: true,
polylines: Set<Polyline>.of(polylines.values),
markers: Set<Marker>.of(markers.values),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
),
);
}
_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 = [];
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
"API_KEY", // My google API key
PointLatLng(_originLatitude, _originLongitude),
PointLatLng(_destLatitude, _destLongitude),
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);
}
}
如何用折线显示我的地图?
问题似乎出在您的 API 密钥上。我 运行 你的代码使用我自己的 API 键,我能够显示多段线(见下面的截图)。
请确保您的项目已link登录到有效的结算帐户,以便使用 Google Maps Platform API。请参阅此 link https://developers.google.com/maps/documentation/directions/quickstart#before_you_begin
的“设置项目”部分的第 2 步