如何解决谷歌地图标记相关问题
我正在编写一个交付应用程序,在我的应用程序中添加了谷歌地图以设置用户位置,并且当我第一次打开地图页面时标记初始值就可以了, 我还需要让用户能够将标记修改为地图上的确切位置, 但我需要为用户保存纬度和经度的新值, 尝试用不同的方法来做,在网上搜索类似的问题,但不能 请帮我解决这个问题。
这是页面代码:
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,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,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',));
}).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,)
],],);
}
} ```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。