Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 放置选择器空结果_Flutter - Fatal编程技术网

Flutter 放置选择器空结果

Flutter 放置选择器空结果,flutter,Flutter,我正在使用选择位置,但结果为空 我使用了提供的示例,但根据下面的照片,我得到了带有“未命名位置”的空结果 void showPlacePicker() async { LocationResult result = await Navigator.of(context).push(MaterialPageRoute( builder: (context) => PlacePicker("AIzaSyBJ4Q0lFgGPr6at5IXD6j

我正在使用选择位置,但结果为空

我使用了提供的示例,但根据下面的照片,我得到了带有“未命名位置”的空结果

  void showPlacePicker() async {
    LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
        builder: (context) =>
            PlacePicker("AIzaSyBJ4Q0lFgGPr6at5IXD6j5YLAFuAKe1Nbo")));
    print(result);
  }
试试这个

Void showPlacePicker() async {
    LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
        builder: (context) =>
            PlacePicker("AIzaSyBJ4Q0lFgGPr6at5IXD6j5YLAFuAKe1Nbo",
                        displayLocation: customLocation,
                        )));
    print(result);
}

除了为API密钥启用Places API、适用于Android的Maps SDK和适用于iOS的Maps SDK外,还应启用地理编码API。我希望文档将得到更新。

按照以下步骤操作,您的问题将得到解决:

  • 在下面的代码中,在PlacePicker()上按CTRL键(对于MAC为cmd),然后导航到PlacePicker.dart文件:

    void showPlacePicker() async {
    LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
        builder: (context) =>
            PlacePicker("YOUR_API_KEY")));
    print(result);
    
    }

  • 库的Inside place_picker.dart文件替换整个方法
    void reverseGeocodeLatLng(LatLng LatLng)async{..}
    与以下各项:

     void reverseGeocodeLatLng(LatLng latLng) async {
    try {
      final response = await http.get(
          "https://maps.googleapis.com/maps/api/geocode/json?" +
              "latlng=${latLng.latitude},${latLng.longitude}&" +
              "key=${widget.apiKey}");
    
      if (response.statusCode != 200) {
        throw Error();
      }
    
      final responseJson = jsonDecode(response.body);
      print('Google maps response body ' + response.body);
    
      if (responseJson['results'] == null) {
        throw Error();
      }
    
      final result = responseJson['results'][0];
    
      setState(() {
        if (result['address_components'] is List &&
            result['address_components'].length >= 7) {
          this.locationResult = LocationResult()
            ..name = result['address_components'][0]['short_name']
            ..locality = result['address_components'][1]['short_name']
            ..latLng = latLng
            ..formattedAddress = result['formatted_address']
            ..placeId = result['place_id']
            ..postalCode = result['address_components'][7]['short_name']
            ..country = AddressComponent.fromJson(
                result['address_components'][6])
            ..administrativeAreaLevel1 = AddressComponent.fromJson(
                result['address_components'][5])
            ..administrativeAreaLevel2 = AddressComponent.fromJson(
                result['address_components'][4])
            ..city = AddressComponent.fromJson(result['address_components'][3])
            ..subLocalityLevel1 = AddressComponent.fromJson(
                result['address_components'][2])
            ..subLocalityLevel2 = AddressComponent.fromJson(
                result['address_components'][1]);
        }
        else {
          this.locationResult = LocationResult()
            ..latLng = latLng
            ..formattedAddress = result['formatted_address']
            ..placeId = result['place_id']
            ..name = result['address_components'][0]['short_name']
            ..locality = result['address_components'][1]['short_name']
            ..administrativeAreaLevel1 = AddressComponent.fromJson(
                result['address_components'][2])
            ..city = AddressComponent.fromJson(result['address_components'][3])
            ..country = AddressComponent.fromJson(
                result['address_components'][4]);
        }
      });
    } catch (e) {
      print(e);
    }
    
    }

  • 关闭应用程序。卸载它并再次运行,您的问题将得到解决

  • 注意:使用place\u picker必须启用Android地图SDK、Places API和地理编码API。


    信用证:

    我想选择当前小工具的位置,但这不起作用。