Flutter 应用程序无法与谷歌地图一起使用

问题描述 投票:0回答:1

E/flutter (23677): [错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:异常:无法获取路由:响应 ---> REQUEST_DENIED E/flutter (23677): #0 NetworkUtil.getRouteBetweenCoordinates (包:flutter_polyline_points/src/network_util.dart:39:9) E/颤振 (23677): E/flutter (23677): #1 PolylinePoints.getRouteBetweenCoordinates (包:flutter_polyline_points/flutter_polyline_points.dart:32:20) E/颤振 (23677): E/flutter (23677): #2 OrderTrackingPageState.getPolyPoints (包:untitled8/news/screen.dart:33:29) E/颤振 (23677): E/颤动(23677):

import 'dart:async';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import 'constants.dart';

class OrderTrackingPage extends StatefulWidget {
  const OrderTrackingPage({Key? key}) : super(key: key);

  @override
  State<OrderTrackingPage> createState() => OrderTrackingPageState();
}

class OrderTrackingPageState extends State<OrderTrackingPage> {
  final Completer<GoogleMapController> _controller = Completer();

  static const LatLng sourceLocation = LatLng(37.33500926, -122.03272188);
  static const LatLng destination = LatLng(37.33429383, -122.06600055);
  List<LatLng> polylineCoordinates = [];
  LocationData? currentLocation;

  void getCurrentLocaiton() {
    Location location = Location();
    location.getLocation().then((location) {
      currentLocation = location;
    });
  }

  Future<void> getPolyPoints() async {
    PolylinePoints polylinePoints = PolylinePoints();
    PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
      google_api_key,
      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(() {});
    }
  }

  @override
  void initState() {
    getCurrentLocaiton();
    getPolyPoints();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'Track Order',
          style: TextStyle(color: Colors.black, fontSize: 16),
        ),
      ),
      body: currentLocation == null
          ? const Center(child: Text("Loading"))
          : GoogleMap(
              initialCameraPosition:
                  CameraPosition(target: LatLng(currentLocation!.latitude!, currentLocation!.longitude!), zoom: 13.5),
              onMapCreated: (GoogleMapController controller) {
                _controller.complete(controller);
              },
              polylines: {
                Polyline(
                  polylineId: PolylineId("route"),
                  points: polylineCoordinates,
                  color: primaryColor, // Set the color of the polyline
                  width: 6, // Set the width of the polyline
                ),
              },
              markers: {
                Marker(
                  markerId: const MarkerId("currentLocati"),
                  position: LatLng(currentLocation!.latitude!, currentLocation!.longitude!),
                ),
                const Marker(
                  markerId: MarkerId("source"),
                  position: sourceLocation,
                ),
                const Marker(
                  markerId: MarkerId("destination"),
                  position: destination,
                ),
              },
            ),
    );
  }
}

我在 youtube 上尝试了视频,但应用程序无法正常工作,我也看不到路线。

android flutter google-maps maps
1个回答
0
投票

虽然这可能有点晚了,但我希望它可以帮助遇到同样问题的其他人。

我遇到了同样的错误,我可以通过转到我的 Google Cloud Console 帐户并在我的

上启用计费来修复它
  • 适用于 Android 的地图 SDK

禁用计费将禁用地图上的路线服务。如果错误仍然存在,请确保也启用这些服务

  • 路线API
  • 距离矩阵API
  • 地理编码API
  • 地理定位API
  • 地点 API
© www.soinside.com 2019 - 2024. All rights reserved.