【Flutter】Google Mapで位置情報権限を許可した後に現在地を示す青丸が表示されない

Dart

import 'dart:async';import 'package:flutter/cupertino.dart';import 'package:flutter/material.dart';import 'package:flutter_riverpod/flutter_riverpod.dart';import 'package:shared_preferences/shared_preferences.dart';import 'package:google_maps_flutter/google_maps_flutter.dart';import 'package:geolocator/geolocator.dart'; import 'package:location_sharing_sns/model/map.dart'; class MapPage extends ConsumerStatefulWidget { const MapPage({Key? key}) : super(key: key); _MapPageState createState() => _MapPageState();} class _MapPageState extends ConsumerState<MapPage> { final Completer<GoogleMapController> _controller = Completer(); StreamSubscription? positionStream; bool _changePosition = false; void initState() { super.initState(); final map = ref.read(mapProvider); map.markers = {}; map.circles = {}; _getLocation(); } void dispose() { positionStream?.cancel(); super.dispose(); } Widget build(BuildContext context) { final map = ref.watch(mapProvider); return Scaffold( body: FutureBuilder( future: map.checkInitialPosition(), builder: (context, initialPositionSnapshot) { if(!initialPositionSnapshot.hasData) { return const Center( child: CupertinoActivityIndicator( radius: 20, ), ); } final isSetInitialPosition = initialPositionSnapshot.data as bool; return Stack( children: [ GoogleMap( mapType: MapType.normal, initialCameraPosition: CameraPosition( target: map.currentPosition, zoom: isSetInitialPosition ? 18 : 5, ), onMapCreated: (GoogleMapController controller) { _controller.complete(controller); }, onCameraMove: (CameraPosition position) { map.centerPosition = position.target; }, onCameraIdle: () { setState(() { _changePosition = true; }); }, markers: map.markers, circles: map.circles, myLocationEnabled: true, myLocationButtonEnabled: false, zoomControlsEnabled: false, ), SafeArea( child: Align( alignment: Alignment.topCenter, child: AnimatedOpacity( opacity: _changePosition ? 1.0 : 0.0, duration: const Duration(milliseconds: 300), child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Colors.white, onPrimary: Colors.black, shape: const StadiumBorder(), splashFactory: InkRipple.splashFactory, ), onPressed: () => _getMarkers(), child: const Text( 'このエリアを検索', style: TextStyle( color: Colors.black, ), ), ), ), ), ), ], ); }, ), floatingActionButton: Column( mainAxisSize: MainAxisSize.min, children: [ Container( height: 50.0, width: 50.0, margin: const EdgeInsets.only(bottom: 10), child: FloatingActionButton( heroTag: "currentPosition", backgroundColor: Colors.white, foregroundColor: Colors.black87, onPressed: () => _getLocation(), child: const Icon(Icons.my_location_outlined), ), ), ], ), ); } _getLocation() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final map = ref.read(mapProvider); // 位置情報のアクセス許可 final checkLocation = await map.checkLocation(); if (checkLocation) { final currentPosition = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.best); prefs.setDouble('currentPositionLatitude', currentPosition.latitude); prefs.setDouble('currentPositionLongitude', currentPosition.longitude); final latLngCurrentPosition = LatLng(currentPosition.latitude, currentPosition.longitude); setState(() { map.currentPosition = latLngCurrentPosition; }); setState(() {}); _moveCamera(latLngCurrentPosition); _getMarkers(); _getLocationStream(); } } _getLocationStream() async { final map = ref.read(mapProvider); const locationSettings = LocationSettings( accuracy: LocationAccuracy.best, ); positionStream = Geolocator.getPositionStream(locationSettings: locationSettings) .listen((Position position) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setDouble('currentPositionLatitude', position.latitude); prefs.setDouble('currentPositionLongitude', position.longitude); final currentPosition = LatLng(position.latitude, position.longitude); await map.setUserCircle(currentPosition); if (map.currentPosition != currentPosition) { map.currentPosition = currentPosition; await _getMarkers(); } }); } _getMarkers() async { final map = ref.watch(mapProvider); final GoogleMapController controller = await _controller.future; final region = await controller.getVisibleRegion(); setState(() { map.getMarkers(context, map.centerPosition!, region); _changePosition = false; }); } _moveCamera(position) async { try { final GoogleMapController controller = await _controller.future; await controller.animateCamera( CameraUpdate.newCameraPosition( CameraPosition( target: position, zoom: 18.0, ), ), ); return true; } catch (e) { return false; } }}

コメントを投稿

0 コメント