Tim*_*ode 17 geolocation google-maps-android-api-2 flutter
我试图在 flutter 中获取用户当前位置,但它显示地图,但不指向我当前的位置。我已在 AndriodManifest 文件中添加了所有必需的权限。
\n这是快照
\n\n这是日志
\nI/Google Maps Android API(24140): Google Play services package version: 201817022\nE/GoogleMapController(24140): Cannot enable MyLocation layer as location permissions are not granted\nD/HostConnection(24140): HostConnection::get() New Host Connection established 0xefab0ec0, tid 25110\nE/flutter (24140): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: User denied permissions to access the device\'s location.\nE/flutter (24140): #0 MethodChannelGeolocator._handlePlatformException\npackage:geolocator_platform_interface/\xe2\x80\xa6/implementations/method_channel_geolocator.dart:242\nE/flutter (24140): #1 MethodChannelGeolocator.getCurrentPosition\npackage:geolocator_platform_interface/\xe2\x80\xa6/implementations/method_channel_geolocator.dart:124\nE/flutter (24140): <asynchronous suspension>\nE/flutter (24140): #2 _locationState.locatepostion\npackage:map_app/abc.dart:26\nE/flutter (24140): <asynchronous suspension>\nW/System (24140): A resource failed to call release.\nRun Code Online (Sandbox Code Playgroud)\n代码
\n Completer<GoogleMapController> _controllerGoogleMap=Completer();\n late GoogleMapController newGoogleMapController;\n double mapbottompadding=0;\n\n GlobalKey<ScaffoldState> scaffoldkey=new GlobalKey<ScaffoldState>();\n late Position currentpositon;\n var geolocator=Geolocator();\n\n void locatepostion() async{\n Position position=await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);\n currentpositon=position; // this is line 26, it is point before await\n\n LatLng latLngPosition=LatLng(position.latitude,position.longitude);\n\n CameraPosition cameraPosition=new CameraPosition(target: latLngPosition,zoom: 14);\n newGoogleMapController.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));\n }\n\n static final CameraPosition googlepostion=CameraPosition(target: LatLng(37.4249,-122.0657));\n\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n body: Stack(children: [\n GoogleMap(\n padding: EdgeInsets.only(bottom: mapbottompadding),\n mapType: MapType.normal,\n myLocationButtonEnabled: true,\n myLocationEnabled: true,\n zoomControlsEnabled: true,\n zoomGesturesEnabled: true,\n initialCameraPosition: googlepostion,\n onMapCreated: (GoogleMapController controller){\n _controllerGoogleMap.complete(controller);\n newGoogleMapController=controller;\n setState(() {\n mapbottompadding=300.0;\n }); \n locatepostion();\n \n\nRun Code Online (Sandbox Code Playgroud)\n这是我的输出
\n\n- - - -更新
\n它在 android 手机上完美运行,但在模拟器上无法运行。
\nRiy*_*ani 34
LocationPermission permission;
permission = await Geolocator.requestPermission();
Run Code Online (Sandbox Code Playgroud)
首先使用此请求权限。
Moh*_*yad 16
class GeolocatorService {
Future<Position?> determinePosition() async {
LocationPermission permission;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
return Future.error('Location Not Available');
}
} else {
throw Exception('Error');
}
return await Geolocator.getCurrentPosition();
}
}
Run Code Online (Sandbox Code Playgroud)
这是地理定位器中出现的常见问题,尤其是。当您未授予设备权限时。默认情况下它是禁用的,因此您必须在locatePosition()调用时请求它。请记住,地理定位器包包装在google_maps_flutter下。因此,要解决位置问题,请确保执行以下操作
class _MapScreenState extends State<MapScreen> {
void locatePosition() async {
bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
await Geolocator.checkPermission();
await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
currentPosition = position;
LatLng latLngPosition = LatLng(position.latitude, position.longitude);
// Ask permission from device
Future<void> requestPermission() async {
await Permission.location.request();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22660 次 |
| 最近记录: |