简体   繁体   English

Flutter 地理定位器 package 未提供英文位置

[英]Flutter geolocator package not giving location in English

I am using Flutter geolocator package latest version and it gives location and names of country in English for most of the countries but for some countries like Germany, Spain, France, etc.. it's not giving names in English rather the native language of the user.我正在使用 Flutter 地理定位器 package 最新版本,它为大多数国家/地区提供了英语的位置和国家名称,但对于某些国家,如德国、西班牙、法国等。它不是用英语命名,而是用户的母语. How can I change this?我怎样才能改变这个?

Position position = await geolocator.getCurrentPosition(
    desiredAccuracy: LocationAccuracy.medium);

List<Placemark> placemark =
    await geolocator.placemarkFromCoordinates(
        position.latitude, position.longitude);
_location = {
  'country': placemark[0].country,
  'postalCode': placemark[0].postalCode,
  'state': placemark[0].administrativeArea,
  'city': placemark[0].subAdministrativeArea,
  'latitude': placemark[0].position.latitude,
  'longitude': placemark[0].position.latitude
};
_city = placemark[0].subAdministrativeArea;

Both the placemarkFromAddress and placemarkFromCoordinates accept an optional localeIdentifier parameter. placemarkFromAddress 和 placemarkFromCoordinates 都接受可选的 localeIdentifier 参数。 This parameter can be used to enforce the resulting placemark to be formatted (and translated) according to the specified locale.此参数可用于强制根据指定的语言环境对生成的地标进行格式化(和翻译)。 The localeIdentifier should be formatted using the syntax: [languageCode]_[countryCode]. localeIdentifier 应使用以下语法进行格式化:[languageCode]_[countryCode]。 Use the ISO 639-1 or ISO 639-2 standard for the language code and the 2 letter ISO 3166-1 standard for the country code.语言代码使用 ISO 639-1 或 ISO 639-2 标准,国家代码使用 2 字母 ISO 3166-1 标准。

Meaning:意义:

List<Placemark> placemark =
    await geolocator.placemarkFromCoordinates(
        position.latitude, position.longitude);

Should be:应该:

List<Placemark> placemark =
    await geolocator.placemarkFromCoordinates(
        position.latitude, position.longitude, localeIdentifier: localeIdentifierValue);

https://pub.dev/packages/geolocator for more information on the options for localeIdentifierValue https://pub.dev/packages/geolocator有关 localeIdentifierValue 选项的更多信息

Placemark has been separated from the geolacator library and currently it is located in the gecoding library Placemark 已从 geolacator 库中分离出来,目前位于地理编码库中

make sure you get their respective libraries from pub.dev确保您从 pub.dev 获得各自的库

import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';

Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    List<Placemark> placemark =
        await placemarkFromCoordinates(position.latitude, position.longitude);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM