谷歌颤抖的谷歌把语言除了建议错误

时间:2020-01-15 13:48:08

标签: flutter google-places-api

我正在使用Google Places软件包。 当我搜索位置时,结果以我的默认电话语言(西班牙语)显示。 当我点击位置时,位置名称会显示在文本字段中,但始终以英文显示。 如何更改语言?

我做了

Prediction p = await PlacesAutocomplete.show(
                    context: context,
                    apiKey: "MY_API_KEY",
                    mode: Mode.overlay, // Mode.fullscreen
                    language: "es",
                    components: [new Component(Component.country, "es")]);
                displayPrediction(p);

Future<Null> displayPrediction(Prediction p) async {
    if (p != null) {
      // get detail (lat/lng)
      PlacesDetailsResponse detail =
          await _places.getDetailsByPlaceId(p.placeId);
      final lat = detail.result.geometry.location.lat;
      final lng = detail.result.geometry.location.lng;
      final locationName = detail.result.name;
      if (detail == null) {
        name = "";
        latitude = 0.0;
        longitude = 0.0;
      } else {
        name = locationName;
        latitude = lat;
        longitude = lng;
        searchLocationController.text = locationName;
        getLocation(lat, lng);
        print(locationName);
        print(lat);
        print(lng);
        FocusScope.of(context).requestFocus(FocusNode()); //Dismisses KEyboard
        //Camera position on new target
        mapController.animateCamera(CameraUpdate.newCameraPosition(
          CameraPosition(
            target: LatLng(
              lat,
              lng,
            ),
            zoom: 15,
          ),
        ));
      }
    }
  }

locationName的打印内容始终为英语

1 个答案:

答案 0 :(得分:0)

https://pub.dev/packages/flutter_google_places

pubspec.yaml

依赖性:颤动: sdk:flutter flutter_google_places:

const kGoogleApiKey = "API_KEY";

Prediction p = await PlacesAutocomplete.show(
                          context: context,
                          apiKey: kGoogleApiKey,
                          mode: Mode.overlay, // Mode.fullscreen
                          language: "es",
                          components: [new Component(Component.country, "es")]);
//to localize the result language apply below code to widget/component

import 'package:flutter_localizations/flutter_localizations.dart';

MaterialApp(
 localizationsDelegates: [
   // ... app-specific localization delegate[s] here
   GlobalMaterialLocalizations.delegate,
   GlobalWidgetsLocalizations.delegate,
 ],
 supportedLocales: [
    const Locale('es'), // Spanish
    // ... other locales the app supports
  ],
  // ...
)