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

问题描述

在我的 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,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,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 部分,所有应用的请求都显示为错误。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)