Raj*_*kad 2 android geolocation flutter google-location-services flutter-dependencies
我有一个博客应用程序,我可以在其中编写全球业务内容以及一些特定国家/地区的内容。比如印度、印度尼西亚等的特别帖子。
这就是为什么我需要一种方法来查找用户来自哪个国家/地区以提供特定内容。
但是,我不想使用 GeoLocator 插件,因为它会询问用户位置。我只想要用户的国家/地区,而不是具体位置,但权限对话框没有指定,这可能会让用户担心为什么这个应用程序需要位置权限。这就是为什么我想要一种方法来获取用户的国家/地区,而无需隐式权限对话框询问其位置。
我尝试了以下方法,但没有一个给出确切的国家/地区名称。
import 'dart:io' show Platform;
String localeName = Platform.localeName;
// this returns language device is using which mostly comes us en_US so this is of no use
Locale myLocale = Localizations.localeOf(context);
// This returned the US as the country despite the App being opened and used in India.
Run Code Online (Sandbox Code Playgroud)
Raj*_*kad 15
好吧,我终于找到了一种简单易行的方法来获取用户的国家/地区。
此链接以 JSON 格式返回用户的位置。因此,现在要做的就是向上述链接/API 发出 Get 请求并将 JSON 解码为 Map。
对于首次安装HTTP包:
Pubspec.yaml:
dependencies:
http:
Run Code Online (Sandbox Code Playgroud)
代码:
import 'package:http/http.dart' as http;
import 'dart:convert';
Response data = await http.get('http://ip-api.com/json');
Map data = jsonDecode(data.body);
String country = data['country'];
Run Code Online (Sandbox Code Playgroud)
编辑:正如好人所提到的,
Free Version Limit is 45 HTTP requests per second from an IP address.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4456 次 |
| 最近记录: |