#flutter #google-maps-markers
Вопрос:
Я пишу приложение для доставки, добавил карту Google в свое приложение, чтобы установить местоположение пользователя, и начальное значение маркера в порядке, когда я открываю страницу карты в первый раз, также мне нужно предоставить пользователю возможность изменять маркер в соответствии с точным положением на карте, но мне нужно сохранить новое значение широты и долготы для пользователя, Пытался сделать это разными методами, искал в Интернете аналогичную проблему и не смог мне в этом помочь, пожалуйста.
Вот код страницы:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:line_icons/line_icons.dart';
class MyMap extends StatefulWidget {
MyMap({
Key key,
}) : super(key: key);
@override
_MyMapState createState() => _MyMapState();
}
class _MyMapState extends State<MyMap> {
Set<Marker> _markers = {};
BitmapDescriptor mapMarker;
GoogleMapController _mapController;
String valueChoose;
Position cl;
double _lat, _long;
MapType mapType = MapType.terrain;
CameraPosition _kGooglePlex;
List mapTypes = ["قمر صناعي", "خريطة"];
Future getPermission() async {
bool services;
LocationPermission permission;
services = await Geolocator.isLocationServiceEnabled();
if (services == false) {
AwesomeDialog(
context: context, title: '', body: Text('Services Not Enabled'))
..show();
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
}
}
Future<void> getLocation() async {
cl = await Geolocator.getCurrentPosition().then((value) => value);
_lat = cl.latitude;
print('$_lat');
_long = cl.longitude;
print('$_long');
_kGooglePlex = CameraPosition(
target: LatLng(_lat, _long),
zoom: 15.5,
);
setState(() {});
}
void _onMapCreated(GoogleMapController controller) {
setState(() {
_mapController = controller;
_markers.add(Marker(
draggable: true,
position: LatLng(_lat, _long),
markerId: MarkerId('markerID1'),
onDragEnd: (value) {
_lat = value.latitude;
print('$_lat');
_long = value.longitude;
print('$_long');
},
));
});
}
void getPlaceMark() async {
List<Placemark> placemarks =
await placemarkFromCoordinates(cl.latitude, cl.longitude);
print(placemarks[0].country);
}
@override
void initState() {
getPermission();
getLocation();
getPlaceMark();
// addData();
super.initState();
}
@override
Widget build(BuildContext context) {
var _size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).backgroundColor,
elevation: 2.5,
leading: Padding(
padding:
const EdgeInsets.only(top: 1.0, left: 5, bottom: 3, right: 0),
child: IconButton(
iconSize: 40,
icon: Icon(LineIcons.utensils),
color: Colors.white,
onPressed: () {
Navigator.of(context).pop();
},
),
),
title: Container(
child: Text(
"Foodie",
style: TextStyle(
letterSpacing: 3,
color: Colors.white,
fontFamily: 'Nicotine',
fontSize: 40),
),
),
),
body: SafeArea(
bottom: false,
child: Column(
children: [
Container(
height: _size.height * 0.08,
child: Center(
child: Text(""),
),
),
Stack(
children: [
Positioned(
top: 0,
right: 0,
child: Container(
padding: EdgeInsets.all(5),
child: DropdownButton(
style: TextStyle(
color: Colors.black,
fontFamily: 'Tajawal',
fontSize: 18,
fontWeight: FontWeight.bold),
iconSize: 25,
value: valueChoose,
onChanged: (val) {
setState(() {
valueChoose = val;
valueChoose == "خريطة"
? mapType = MapType.terrain
: mapType = MapType.satellite;
});
},
items: mapTypes.map((e) {
return DropdownMenuItem(
value: e,
child: Text(
e,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Tajawal',
fontSize: 18,
fontWeight: FontWeight.bold),
));
}).toList(),
),
),
),
Container(
height: _size.height * 0.75,
width: _size.width,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
),
child: _kGooglePlex == null
? Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).backgroundColor,
color: Theme.of(context).primaryColor,
strokeWidth: 12,
),
)
: GoogleMap(
markers: _markers,
mapType: mapType,
initialCameraPosition: _kGooglePlex,
onMapCreated: _onMapCreated,
),
)
],
),
],
),
),
);
}
} ```