Flutter 颤振多段线问题

Flutter 颤振多段线问题,flutter,google-maps,google-maps-markers,flutter-dependencies,google-polyline,Flutter,Google Maps,Google Maps Markers,Flutter Dependencies,Google Polyline,我目前在谷歌地图公司工作。我遵循GCP的所有步骤,并为谷歌地图启用方向API。现在我想在地图上显示源和目标之间的多段线。但当我在真实设备上运行应用程序时,它无法显示。我只得到来源和目的地的标记。 请参阅下面的代码 import 'package:flutter/material.dart'; import 'package:flutter_polyline_points/flutter_polyline_points.dart'; import 'dart:async'; import 'pac

我目前在谷歌地图公司工作。我遵循GCP的所有步骤,并为谷歌地图启用方向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);
}
} 
导入“包装:颤振/材料.省道”;
导入“包:颤振多段线点/颤振多段线点.省道”;
导入“dart:async”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
双原纬度=6.5212402;
双原经度=3.3679965;
双纬度=6.849660;
双经度=3.648190;
地图标记={};
多段线点多段线点=多段线点();
映射多段线={};
类MyAppState扩展了状态{
Completer _controller=Completer();
静态最终摄像机位置_kGooglePlex=摄像机位置(
目标:纬度(_原始纬度,_原始经度),
缩放:9.4746,
);
@凌驾
void initState(){
_添加标记(
纬度(_原始纬度,_原始经度),
“起源”,
BitmapDescriptor.defaultMarker,
);
_添加标记(
LatLng(_dest纬度,_dest经度),
“目的地”,
BitmapDescriptor.defaultMarkerWithHue(90),
);
_getPolyline();
打印(“在getpoly输入”);
super.initState();
} 
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
标题:“欢迎来到弗利特”,
家:脚手架(
正文:谷歌地图(
mapType:mapType.normal,
initialCameraPosition:_kGooglePlex,
myLocationEnabled:对,
倾斜姿势启用:正确,
同谋:是的,
scrollGesturesEnabled:正确,
zoomGesturesEnabled:对,
多段线:一组(多段线值),
标记:一组(标记值),
onMapCreated:(谷歌地图控制器){
_控制器。完成(控制器);
},
),
),
);
}
_addMarker(LatLng位置、字符串id、位图描述符){
MarkerId MarkerId=MarkerId(id);
标记器=
标记(markerId:markerId,图标:描述符,位置:position);
markers[markerId]=标记器;
}
_添加多段线(列出多段线坐标){
多段线id=多段线id(“多边形”);
多段线多段线=多段线(
多段线id:id,
点:多段线坐标,
宽度:8,
);
多段线[id]=多段线;
setState((){});
}
void\u getPolyline()异步{
列出多段线坐标=[];
PolylineResult结果=等待polylinePoints.GetRouteBeween坐标(
“API_密钥”//My google API密钥
点定位(_原始纬度,_原始经度),
点定位(_dest纬度,_dest经度),
travelMode:travelMode.driving,
);
if(result.points.isNotEmpty){
结果.points.forEach((PointLatLng点){
多段线坐标.add(LatLng(点.纬度,点.经度));
});
}否则{
打印(result.errorMessage);
}
_添加多段线(多段线坐标);
}
} 

如何使用多段线显示地图?

问题似乎出在您的API密钥上。我使用自己的API键运行您的代码,并且能够显示多段线(参见下面的屏幕截图)

请确保您的项目链接到有效的计费帐户,以便使用谷歌地图平台API。请参阅此链接“设置项目”部分的步骤2


请不要将实际的API密钥放在此处的任何代码中。公开暴露不安全的密钥可能会导致意外使用,这可能会导致您的帐户发生意外费用。请不要(仅)发布错误、代码和依赖项的图像。非常感谢提供有价值的信息@NelsonJr。谢谢您提供的信息。我将删除它@geocodezipThanks一吨为您宝贵的答案。这对我帮助很大……!:)