Showing
1 changed file
with
27 additions
and
5 deletions
| 1 | +import 'dart:async'; | ||
| 2 | +import 'dart:math'; | ||
| 3 | + | ||
| 1 | import 'package:location/location.dart'; | 4 | import 'package:location/location.dart'; |
| 2 | import 'package:flutter/material.dart'; | 5 | import 'package:flutter/material.dart'; |
| 3 | import 'package:Parlando/widgets/my_button.dart'; | 6 | import 'package:Parlando/widgets/my_button.dart'; |
| ... | @@ -22,10 +25,14 @@ class AddressSelectPageState extends State<AddressSelectPage> { | ... | @@ -22,10 +25,14 @@ class AddressSelectPageState extends State<AddressSelectPage> { |
| 22 | LatLng _center = const LatLng(45.521563, -122.677433); | 25 | LatLng _center = const LatLng(45.521563, -122.677433); |
| 23 | late GoogleMapController mapController; | 26 | late GoogleMapController mapController; |
| 24 | bool isLoading = false; | 27 | bool isLoading = false; |
| 28 | + int _markerIdCounter = 1; | ||
| 29 | + Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; | ||
| 30 | + late StreamSubscription _locationSubscription; | ||
| 25 | 31 | ||
| 26 | @override | 32 | @override |
| 27 | void dispose() { | 33 | void dispose() { |
| 28 | _controller.dispose(); | 34 | _controller.dispose(); |
| 35 | + _locationSubscription.cancel(); | ||
| 29 | super.dispose(); | 36 | super.dispose(); |
| 30 | } | 37 | } |
| 31 | 38 | ||
| ... | @@ -40,7 +47,6 @@ class AddressSelectPageState extends State<AddressSelectPage> { | ... | @@ -40,7 +47,6 @@ class AddressSelectPageState extends State<AddressSelectPage> { |
| 40 | 47 | ||
| 41 | bool serviceEnabled; | 48 | bool serviceEnabled; |
| 42 | PermissionStatus permissionGranted; | 49 | PermissionStatus permissionGranted; |
| 43 | - | ||
| 44 | serviceEnabled = await location.serviceEnabled(); | 50 | serviceEnabled = await location.serviceEnabled(); |
| 45 | if (!serviceEnabled) { | 51 | if (!serviceEnabled) { |
| 46 | serviceEnabled = await location.requestService(); | 52 | serviceEnabled = await location.requestService(); |
| ... | @@ -48,7 +54,6 @@ class AddressSelectPageState extends State<AddressSelectPage> { | ... | @@ -48,7 +54,6 @@ class AddressSelectPageState extends State<AddressSelectPage> { |
| 48 | return; | 54 | return; |
| 49 | } | 55 | } |
| 50 | } | 56 | } |
| 51 | - | ||
| 52 | permissionGranted = await location.hasPermission(); | 57 | permissionGranted = await location.hasPermission(); |
| 53 | if (permissionGranted == PermissionStatus.denied) { | 58 | if (permissionGranted == PermissionStatus.denied) { |
| 54 | permissionGranted = await location.requestPermission(); | 59 | permissionGranted = await location.requestPermission(); |
| ... | @@ -57,9 +62,25 @@ class AddressSelectPageState extends State<AddressSelectPage> { | ... | @@ -57,9 +62,25 @@ class AddressSelectPageState extends State<AddressSelectPage> { |
| 57 | } | 62 | } |
| 58 | } | 63 | } |
| 59 | 64 | ||
| 65 | + _locationSubscription = | ||
| 60 | location.onLocationChanged.listen((LocationData currentLocation) { | 66 | location.onLocationChanged.listen((LocationData currentLocation) { |
| 61 | - // _center = LatLng(currentLocation.latitude!, currentLocation.longitude!); | 67 | + _center = LatLng(currentLocation.latitude!, currentLocation.longitude!); |
| 62 | - setState(() {}); | 68 | + |
| 69 | + final String markerIdVal = 'marker_id_$_markerIdCounter'; | ||
| 70 | + _markerIdCounter++; | ||
| 71 | + final MarkerId markerId = MarkerId(markerIdVal); | ||
| 72 | + | ||
| 73 | + final Marker marker = Marker( | ||
| 74 | + markerId: markerId, | ||
| 75 | + position: LatLng(currentLocation.latitude!, currentLocation.longitude!), | ||
| 76 | + ); | ||
| 77 | + | ||
| 78 | + LatLng sydney = | ||
| 79 | + LatLng(currentLocation.latitude!, currentLocation.longitude!); | ||
| 80 | + mapController.moveCamera(CameraUpdate.newLatLng(sydney)); | ||
| 81 | + setState(() { | ||
| 82 | + markers[markerId] = marker; | ||
| 83 | + }); | ||
| 63 | }); | 84 | }); |
| 64 | } | 85 | } |
| 65 | 86 | ||
| ... | @@ -90,8 +111,9 @@ class AddressSelectPageState extends State<AddressSelectPage> { | ... | @@ -90,8 +111,9 @@ class AddressSelectPageState extends State<AddressSelectPage> { |
| 90 | onMapCreated: _onMapCreated, | 111 | onMapCreated: _onMapCreated, |
| 91 | initialCameraPosition: CameraPosition( | 112 | initialCameraPosition: CameraPosition( |
| 92 | target: _center, | 113 | target: _center, |
| 93 | - zoom: 11.0, | 114 | + zoom: 16.0, |
| 94 | ), | 115 | ), |
| 116 | + markers: Set<Marker>.of(markers.values), | ||
| 95 | ), | 117 | ), |
| 96 | ), | 118 | ), |
| 97 | Expanded( | 119 | Expanded( | ... | ... |
-
Please register or login to post a comment