result.status == 'OK' 使用 google 方向 API 在 flutter 中的折线期间不执行部分

result.status == 'OK' part is not executed during polyline in flutter using google directiions API

在我的 flutter 应用程序中,只有标记在地图屏幕中可见,但路径未通过多段线显示,我设置了一个调试器点并未执行以下 if 语句。

if(result.status == 'OK'){
  result.points.forEach((PointLatLng point){
    polylineCoordinates.add(LatLng(point.latitude,point.longitude));
  });

请参考下面的代码,上面是从下面的代码中提取的。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class GooglePolylines extends StatefulWidget {
  GooglePolylines({Key key}) : super(key: key);
  @override
 GooglePolyline createState() => GooglePolyline();
  
}

class GooglePolyline extends State<GooglePolylines>{
  GoogleMapController mapController;
  double _originLatitude = 26.48424, _originLongitude = 50.04551;
  double _destLatitude = 26.46423, _destLongitude = 50.06358;
  Map<MarkerId, Marker> markers = {};
 
  String googleAPiKey = "MY KEY";
  Set <Polyline> _polyLines = Set<Polyline>(); 
  List <LatLng> polylineCoordinates = [];
  PolylinePoints polylinePoints ;
  
  @override
  void initState() {
    super.initState();
    polylinePoints =PolylinePoints();

    /// origin marker
    _addMarker(LatLng(_originLatitude, _originLongitude), "origin",
        BitmapDescriptor.defaultMarker);

    /// destination marker
    _addMarker(LatLng(_destLatitude, _destLongitude), "destination",
        BitmapDescriptor.defaultMarkerWithHue(90));
        // setPolyLines();
    // _getPolyline();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: GoogleMap(
        initialCameraPosition: CameraPosition(
            target: LatLng(_originLatitude, _originLongitude), zoom: 15),
        myLocationEnabled: true,
        tiltGesturesEnabled: true,
        compassEnabled: true,
        scrollGesturesEnabled: true,
        zoomGesturesEnabled: true,
        onMapCreated: _onMapCreated,
        markers: Set<Marker>.of(markers.values),
      //rendering polyLines by map
      polylines: _polyLines,
      )),
    );
  }

  void _onMapCreated(GoogleMapController controller) async {
    mapController = controller;
    setPolyLines();
  }

  _addMarker(LatLng position, String id, BitmapDescriptor descriptor) {
    MarkerId markerId = MarkerId(id);
    Marker marker =
        Marker(markerId: markerId, icon: descriptor, position: position);
    markers[markerId] = marker;
  }



  void setPolyLines() async {

    PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
      googleAPiKey, 
      PointLatLng( _originLatitude, _originLongitude), 
      PointLatLng(_destLatitude, _destLongitude ),
      travelMode: TravelMode.driving,
      
      
    );
   
    if(result.status == 'OK'){
      result.points.forEach((PointLatLng point){
        polylineCoordinates.add(LatLng(point.latitude,point.longitude));
      });
      setState((){
        _polyLines.add(Polyline(
          visible: true,
          width:10,
          polylineId:PolylineId('polyLineTest'),
         color: Colors.blue,
          points:polylineCoordinates
        ));
      });
    }
  }
}

在API部分google云平台控制台,所有申请的请求都显示为错误。

我使用我自己的 API 键尝试了您的代码,我能够显示折线(请参见下面的屏幕截图)。看起来问题出在您自己的 API 密钥上。要解决此问题,请确保:

  • 您使用的是有效的 API 密钥并且
  • 路线 API 已在您的项目中启用并且
  • 您的项目已链接到有效的结算帐户

查看此 documentation 了解更多信息。