Невозможно получить разрешение на определение местоположения AndroidAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Невозможно получить разрешение на определение местоположения Android

Сообщение Anonymous »

У меня проблема с проектом Flutter, который я делаю. Я использую API Googlemaps в качестве карты, и пока карта загружается, но я не могу получить свое текущее местоположение. Я вызываю свой метод initLocation, чтобы получить разрешение на свое местоположение и получить свою позицию, как только мое представление загружается, но оно почему-то не вызывается, и я не понимаю, почему.
Вот мое представление
class GoogleMapWidget extends StatefulWidget {
const GoogleMapWidget({super.key});

@override
State createState() => GoogleMapState();
}

class GoogleMapState extends State {
late GoogleMapController mapController;
final LatLng _center = const LatLng(45.521563, -122.677433);

void onMapCreated(GoogleMapController controller) {
mapController = controller;
}

@override
Widget build(BuildContext context) {
return ViewModelBuilder.reactive(
viewModelBuilder: () => GoogleMapViewModel(),
onViewModelReady: (viewModel) async {
await viewModel.initLocation();
},
builder: (context, viewModel, child) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: [
GoogleMap(
onMapCreated: onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
Positioned(
bottom: 16.0,
left: 16.0,
child: FloatingActionButton(
onPressed: viewModel.locationEnabled
? () => viewModel.recenterMap(mapController)
: null,
child: Icon(
!viewModel.locationEnabled
? Icons.gps_off_outlined
: (viewModel.isCentered
? Icons.gps_fixed_outlined
: Icons.gps_not_fixed_outlined),
),
),
),
],
),
),
);
},
);
}
}
< /code>
И вот мой метод 2: тот, который обрабатывает разрешение, и тот, который вызывается на загрузке. < /p>
Future _handlePermission() async {
bool serviceEnabled;
LocationPermission permission;

serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
await Geolocator.openLocationSettings();
return false;
}

permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return false;
}
}

if (permission == LocationPermission.deniedForever) {
return false;
}

return true;
}
< /code>
Future initLocation(GoogleMapController mapController) async {
setBusy(true);
final hasPermission = await _handlePermission();

if (!hasPermission) {
_locationEnabled = false;
notifyListeners();
setBusy(false);
return;
}

try {
final position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

_currentPosition = LatLng(position.latitude, position.longitude);
_isCentered = true;

// Déplacer la caméra vers la position actuelle
await mapController.animateCamera(
CameraUpdate.newLatLngZoom(_currentPosition!, 15.5),
);
} catch (e) {
_currentPosition = defaultLatLng;
_isCentered = true;

await mapController.animateCamera(
CameraUpdate.newLatLngZoom(defaultLatLng, 15.5),
);
notifyListeners();
}

setBusy(false);
}


Подробнее здесь: https://stackoverflow.com/questions/793 ... permission
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»