颤振地理定位器位置无法为真实手机获取



>我为我的小应用程序使用了地理定位器颤振包,它与模拟器完美配合,但是当我尝试在真正的智能手机中使用时,它不起作用。你有什么想法吗?

void getLocationData() async {
WeatherModel weatherModel = WeatherModel();
var weatherData = await weatherModel.getLocationWeather();
//print(weatherData);
var zipCode = await weatherModel.getLocationZipCode();
zipCode = zipCode.toString().substring(0, 2);
Navigator.push(context, MaterialPageRoute(builder: (context) {
return LocationScreen(
locationWeather: weatherData,
zipCode: zipCode,
);
}));
}
Future<dynamic> getLocationWeather() async {
Location location = Location();
await location.getCurrentLocation();
NetworkHelper networkHelper = NetworkHelper(
'$openWeatherMapURL?lat=${location.latitude}&lon=${location.longitude}&appid=$apiKey&units=metric');
var weatherData = await networkHelper.getData();
return weatherData;
}

Angela Yu的学生? 她的项目需要一次更新来优化和修复小错误。

我在使用地理定位器时遇到了问题,它没有被正确使用,这个网站对我帮助很大。然后我用提供者和...生活再次变得美好。

尝试使用地理定位器。我似乎您没有使用地理定位器包

final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
// return true if location service is enable, false if not
bool locationServiceEnable = await geolocator.isLocationServiceEnabled();
// position instace
Position position;
// if location service enable get current position
if (locationServiceEnable) {
// await current position
position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
// if location service is not enable get last known position
} else {
// await last known position
position = await geolocator.getLastKnownPosition(
desiredAccuracy: LocationAccuracy.best);
}
if (position != null) {
// fetch weather data with position data
}

你应该添加

<uses-permission android:name="android.permission.INTERNET" />

到 AndroidManifest.xml。它有助于使用互联网在Android设备中获取位置数据,因为您的应用仍然需要从URL openweathermap获取数据。

使用地理定位器: ^7.0.1
并添加

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> 

到 AndroidManifest.xml 文件

void getLocation() async {
print('getLocation');
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
}

并在按下时使用

onPressed: () {
getLocation();
},

解决了问题。

最新更新