如何在颤动中获取用户的位置 [英] how to get a user's location in flutter
问题描述
我想定位用户并通过api将他的位置坐标发送到服务器,请用代码回答,我试过图书馆地理定位器:^7.7.0
I want to locate the user and send his location coordinates via api to the server, Please answer with code, I've tried library geolocator: ^7.7.0
void getCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var lat = position.latitude;
var long = position.longitude;
print("Latitude: $lat and Longitude: $long");
}
此代码不起作用,错误:
This code is not working , Error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method getCurrentPosition on channel flutter.baseflow.com/geolocator)
E/flutter (25476): #0 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:156
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #1 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:128:27)
E/flutter (25476): <asynchronous suspension>
E/flutter (25476): #2 getCurrentLocation
package:check_time/Screens/fingerprint_Screen.dart:84
E/flutter (25476): <asynchronous suspension>
推荐答案
试试下面的代码希望对你有帮助,参考 geolocator 这里和地理编码这里获取纬度和经度以及您的位置名称
Try below code hope its helpful to you, refer geolocator here and geocoding here for getting the latitude and longitude and your location name
为您的当前位置创建 Position 和一个变量以及 init()
mathod
Create Position and one variable for your current position and init()
mathod
Position? _currentPosition;
String? _currentAddress;
void initState() {
super.initState();
getCurrentLocation();
}
获取经纬度函数
getCurrentLocation() {
Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
forceAndroidLocationManager: true)
.then((Position position) {
setState(() {
_currentPosition = position;
print(position.latitude);
print(position.longitude);
_getAddressFromLatLng();
});
}).catchError((e) {
print(e);
});
}
根据您的纬度和经度定位的功能
Function for location from your latitude and longitude
_getAddressFromLatLng() async {
try {
List<Placemark> placemarks = await placemarkFromCoordinates(
_currentPosition!.latitude, _currentPosition!.longitude);
Placemark place = placemarks[0];
setState(() {
_currentAddress = "${place.subLocality}";//here you can used place.country and other things also
print(_currentAddress);
});
} catch (e) {
print(e);
}
}
您的小部件:
Column(
children: [
if (_currentPosition != null && _currentAddress != null)
Text(
_currentAddress!,
style: TextStyle(
fontWeight: FontWeight.bold,
letterSpacing: .5,
fontSize: 15,
color: Colors.black,
),
),
],
)
在 application
标签上方的 androidappsrcmainAndroidManifest.xml
文件中添加以下行
Add below lines inside androidappsrcmainAndroidManifest.xml
file above of the application
tag
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
转到 androidappuild.gradle
文件并更改以下更改
Go to androidappuild.gradle
file and change below changes
compileSdkVersion 31
minSdkVersion 21
targetSdkVersion 29
这篇关于如何在颤动中获取用户的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!