android - 如何在颤动中设置中心()传单地图
问题描述
我正在制作传单地图。我构建了标记生成器以使它们动态移动。我有一个按钮可以使地图以我确定的坐标为中心。
这是创建地图的小部件。
Widget _initMap(BuildContext context){
return Stack(
children: <Widget>[
new FlutterMap(
options: MapOptions(
minZoom: _minzoom,
maxZoom: _maxzoom,
center: LatLng(mylatitude,mylongitude),
),
layers: [
new TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c']),
new MarkerLayerOptions(
markers: _generateMarker(context)
),
]
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: EdgeInsets.all(20.0),
child: new FloatingActionButton(
onPressed: (){_initMap(context);},
child: Icon(Icons.gps_fixed),
),
),
),
],
);
}
我希望按钮重新创建小部件 _initMap 以便地图将根据变量 mylatitude 和 mylongitude 重置其中心,因为这两个变量是动态变化的。有人知道怎么做吗?
解决方案
您必须将 MapController 传递给您的 Flutter FlutterMap 小部件,并在您的 onPressed 触发.move()
控制器的方法。
当您单击 FAB 时,以下代码可让您从初始中心移动到埃菲尔铁塔。只需在应用程序启动或任何时候初始化您的地图。
MapController _mapController = MapController();
Widget _initMap(BuildContext context){
return Stack(
children: <Widget>[
new FlutterMap(
mapController: _mapController,
options: MapOptions(
minZoom: _minzoom,
maxZoom: _maxzoom,
center: LatLng(mylatitude,mylongitude),
),
layers: [
new TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c']),
new MarkerLayerOptions(
markers: _generateMarker(context)
),
]
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: EdgeInsets.all(20.0),
child: new FloatingActionButton(
onPressed: (){
_mapController.move(LatLng(48.8587372,2.2945457), 13);
},
child: Icon(Icons.gps_fixed),
),
),
),
],
);
}
推荐阅读
- python - 跳过每组中的前 n 行
- sql-server - .Net Framwork Web App 执行超时已过期
- android - 无法在真正的 Android 设备上运行 React Native 项目
- python - 按下按键后,pygame 角色不会移动
- asp.net-mvc - IIS 8.5:服务应用程序池的进程'
' 遇到与 Windows 进程激活服务的致命通信错误 - python - 为什么我的 Python 只读取 csv 文件而不读取 xlsx 文件?
- visual-studio - HTML AgilityPack 问题 - Visual Basic - UTF-8
- javascript - 使用 AJAX Javascript 发送纯文本表单数据
- python - 稍后出现 if 子句时未引用 python 中的全局变量
- python - 无法分配“[{'id': '2'}]”:“Model .field”必须是“ForeignKey”实例