通过 Google 地图 API 反向地理编码 [REQUEST_DENIED]

Reverse Geocoding [REQUEST_DENIED] by Google Maps API

我的 VALID 和 ENABLED Google API 密钥在我请求反向地理编码时生成来自地图地理编码 API 的 REQUEST_DENIED 响应。相同的 GOOGLE_API_KEY 与 Maps Static API 和 Maps SDK for Android API 在同一个项目中完美运行。是的,我启用了地理编码 API 进行计费。

我不明白为什么它不起作用,请帮助。

我的代码:

static Future<String> getPlaceAddress(double lat, double lng) async {
    final url = Uri.parse(
        'https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$lng&amp;key=$GOOGLE_API_KEY');

    final response = await http.get(url);
    
    return json.decode(response.body)['results'][0]['formatted_address'];
  }

控制台上的错误消息:

Performing hot reload...
Syncing files to device sdk gphone x86...
Reloaded 1 of 960 libraries in 533ms.
D/MediaScannerConnection(10267): Scanned /data/user/0/com.example.dm_places/cache/d3bef27d-313a-4d37-8159-035faf606d438754135540374763487.jpg to null
D/EGL_emulation(10267): eglCreateContext: 0xf4525150: maj 2 min 0 rcv 2
I/flutter (10267): a
I/flutter (10267): File: '/data/user/0/com.example.dm_places/app_flutter/scaled_d3bef27d-313a-4d37-8159-035faf606d438754135540374763487.jpg'
I/flutter (10267): {error_message: You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account, results: [], status: REQUEST_DENIED}
E/flutter (10267): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter (10267): #0      List.[] (dart:core-patch/growable_array.dart:260:36)
E/flutter (10267): #1      LocationHelper.getPlaceAddress (package:dm_places/helpers/location_helper.dart:19:49)
E/flutter (10267): <asynchronous suspension>
E/flutter (10267): #2      Places.addPlace (package:dm_places/providers/places.dart:21:21)
E/flutter (10267): <asynchronous suspension>
E/flutter (10267): 

错误消息表明您没有在请求中使用 API 键。好像是因为请求中的amp;所以最后的key参数被省略了。删除 amp;,您的请求将起作用。