Flutter 如何通过在地图上录音设置标记?
如何通过在Flitter中录制地图来设置标记? 如果我点击按钮设置一个标记,一个标记将被设置到地图的中心。如何通过在地图上键入来将其更改为设置标记。我尝试了很多东西,我看了很多教程,但它们没有展示如何做到这一点。我还尝试将标记设置为用户位置,但它不起作用。如果方便的话,请告诉我怎么做Flutter 如何通过在地图上录音设置标记?,flutter,markers,Flutter,Markers,如何通过在Flitter中录制地图来设置标记? 如果我点击按钮设置一个标记,一个标记将被设置到地图的中心。如何通过在地图上键入来将其更改为设置标记。我尝试了很多东西,我看了很多教程,但它们没有展示如何做到这一点。我还尝试将标记设置为用户位置,但它不起作用。如果方便的话,请告诉我怎么做 import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'dart:async'; impor
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
GoogleMapController mapController;
class MapsDemo extends StatefulWidget {
MapsDemo() : super();
@override
MapsDemoState createState() => MapsDemoState();
}
class MapsDemoState extends State<MapsDemo> {
//
Completer<GoogleMapController> _controller = Completer();
static const LatLng _center = const LatLng(51, 10);
final Set<Marker> _markers = {};
LatLng _lastMapPosition = _center;
MapType _currentMapType = MapType.normal;
void _currentLocation() async {
final GoogleMapController controller = await _controller.future;
LocationData currentLocation;
var location = new Location();
try {
currentLocation = await location.getLocation();
} on Exception {
currentLocation = null;
}
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 0,
target: LatLng(currentLocation.latitude, currentLocation.longitude),
zoom: 18.0,
),
));
}
_onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
}
_onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
_onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.hybrid
: MapType.normal;
});
}
_onAddMarkerButtonPressed() {
_currentLocation();
setState(() {
_markers.add(
Marker(
markerId: MarkerId(_lastMapPosition.toString()),
position: _lastMapPosition,
infoWindow: InfoWindow(
title: "Pizza Parlour",
snippet: "This is a snippet",
onTap: (){
}
),
onTap: (){
},
icon: BitmapDescriptor.defaultMarker));
});
}
Widget button(Function function, IconData icon) {
return FloatingActionButton(
onPressed: function,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.blue,
child: Icon(
icon,
size: 36.0,
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
resizeToAvoidBottomPadding: false,
appBar: PreferredSize(
preferredSize: Size.fromHeight(40),
child: AppBar(
automaticallyImplyLeading: false,
centerTitle: true,
title: Column(
children: <Widget>[
const Text("Viewist", style: TextStyle(fontSize: 30.0),textAlign: TextAlign.center,),
const Text("", style: TextStyle(fontSize: 13.0),textAlign: TextAlign.center,),
],
),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Colors.blue,
Colors.lightBlueAccent
])
),
),
),
),
body:
Stack(
children: <Widget>[
GoogleMap(
padding: new EdgeInsets.all(3.0),
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 6.0,
),
mapType: _currentMapType,
markers: _markers,
myLocationEnabled: true,
myLocationButtonEnabled: false,
onCameraMove: _onCameraMove,
),
Padding(
padding: EdgeInsets.all(20.0),
child: Align(
alignment: Alignment.topRight,
child: Column(
children: <Widget>[
button(_onMapTypeButtonPressed, Icons.map),
SizedBox(
height: 18.0,
),
button(_onAddMarkerButtonPressed, Icons.add_location),
SizedBox(
height: 18.0,
),
button(_currentLocation, Icons.location_searching),
],
),
),
),
],
),
drawer: Drawer(
child: ListView(
padding: new EdgeInsets.all(0.0),
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
gradient: LinearGradient(colors: <Color>[
Colors.blue,
Colors.lightBlueAccent
])
),
child: Container(
child: Column(
children: <Widget>[
Material(
borderRadius: BorderRadius.all(Radius.circular(40.0)),
elevation: 10,
child: Padding(padding: EdgeInsets.all(8.0),
child: Image.asset('images/a.png',width: 80,height: 80,),
),
),
Padding(padding: EdgeInsets.all(2.0), child: Text ('Viewist', style: TextStyle(color: Colors.white, fontSize: 30.0,),
)
)],
),
)),
CustomListTile(Icons.person, 'Profile', ()=>{}),
CustomListTile(Icons.notifications, 'Notification', ()=>{}),
CustomListTile(Icons.settings, 'Settings', ()=>{}),
CustomListTile(Icons.lock, 'Log Out', ()=>{}),
],
),
),
),
);
}
}
class CustomListTile extends StatelessWidget {
IconData icon;
String text;
Function onTap;
CustomListTile(this.icon,this.text,this.onTap);
@override
Widget build(BuildContext context) {
// TODO: implement createState
return Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey.shade400))
),
child: InkWell(
splashColor: Colors.lightBlueAccent,
onTap: onTap,
child: Container(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(icon),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(text, style: TextStyle(
fontSize: 16.0
),),
),
],
),
Icon(Icons.arrow_right)
],
),
),
),
),
);
}
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
导入“dart:async”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
导入“package:location/location.dart”;
谷歌地图控制器;
类MapsDemo扩展StatefulWidget{
MapsDemo():super();
@凌驾
MapsDemoState createState()=>MapsDemoState();
}
类MapsDemoState扩展了状态{
//
Completer _controller=Completer();
静态恒速中心=恒速(51,10);
最终集_标记={};
LatLng _lastmaposition=_center;
MapType _currentMapType=MapType.normal;
void\u currentLocation()异步{
最终GoogleMapController=wait_controller.future;
位置数据当前位置;
var location=新位置();
试一试{
currentLocation=等待位置。getLocation();
}例外{
currentLocation=null;
}
controller.animateCamera(CameraUpdate.newCameraPosition(
摄像定位(
方位:0,,
目标:LatLng(currentLocation.latitude,currentLocation.longitude),
缩放:18.0,
),
));
}
_onMapCreated(谷歌地图控制器){
_控制器。完成(控制器);
}
_onCameraMove(摄像机位置){
_lastMapPosition=position.target;
}
_onMapTypeButtonPressed(){
设置状态(){
_currentMapType=\u currentMapType==MapType.normal
?MapType.hybrid
:MapType.normal;
});
}
_onAddMarkerButtonPressed(){
_currentLocation();
设置状态(){
_markers.add(
标记(
markerId:markerId(_lastmaposition.toString()),
位置:_lastmaposition,
信息窗口:信息窗口(
标题:“比萨店”,
片段:“这是一个片段”,
onTap:(){
}
),
onTap:(){
},
图标:BitmapDescriptor.defaultMarker);
});
}
小部件按钮(功能,图标数据图标){
返回浮动操作按钮(
onPressed:函数,
MaterialTargetSize:MaterialTargetSize.padded,
背景颜色:Colors.blue,
子:图标(
偶像
尺寸:36.0,
),
);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
家:脚手架(
resizeToAvoidBottomPadding:false,
appBar:首选大小(
首选尺寸:尺寸。从高度(40),
孩子:AppBar(
自动嵌入:false,
标题:对,
标题:专栏(
儿童:[
常量文本(“视图”,样式:TextStyle(fontSize:30.0),textAlign:textAlign.center,),
常量文本(“,样式:TextStyle(fontSize:13.0),textAlign:textAlign.center,),
],
),
flexibleSpace:容器(
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topLeft,
结束:对齐。右下角,
颜色:[
颜色,蓝色,
颜色。浅蓝色口音
])
),
),
),
),
正文:
堆叠(
儿童:[
谷歌地图(
填充:新边缘设置。全部(3.0),
onMapCreated:_onMapCreated,
initialCameraPosition:CameraPosition(
目标:_中心,
缩放:6.0,
),
mapType:\u currentMapType,
标记:_标记,
myLocationEnabled:对,
myLocationButtonEnabled:false,
onCameraMove:_onCameraMove,
),
填充物(
填充:所有边缘设置(20.0),
子对象:对齐(
对齐:alignment.topRight,
子:列(
儿童:[
按钮(_onMapTypeButtonPressed,Icons.map),
大小盒子(
身高:18.0,
),
按钮(_onaddmarker按钮按下,图标。添加位置),
大小盒子(
身高:18.0,
),
按钮(\u当前位置,图标。位置\u搜索),
],
),
),
),
],
),
抽屉(
子:ListView(
填充:新边集。全部(0.0),
儿童:[
抽屉阅读器(
装饰:盒子装饰(
渐变:线性渐变(颜色:[
颜色,蓝色,
颜色。浅蓝色口音
])
),
子:容器(
子:列(
儿童:[
材料(
borderRadius:borderRadius.all(半径圆形(40.0)),
标高:10,
子项:填充(填充:EdgeInsets.all(8.0)),
子项:Image.asset('images/a.png',宽度:80,高度:80,),
),
),
Padding(Padding:EdgeInsets.all(2.0),child:Text('viewsist',样式:
final Set<Marker> _markers = {};
List<Marker> _markers = [];
GoogleMap(
//... others properties
onTap: (LatLng point) {
print('tapped');
setState(() {
_markers.add(Marker(
markerId: MarkerId(point.toString()),
position: point,
icon: BitmapDescriptor.defaultMarker,
));
});
},
)