google-maps - 在运行地图时遇到问题,无一例外 googleMaps&Flutter
问题描述
每次我尝试打开地图时都会收到此错误
我不知道为什么会这样
我在网上搜索解决方案虽然还没有找到
它还说-在构建 CreateMapMarker(dirty, state: _CreateMapMarkerState#6a02b) 时抛出了以下断言:
感谢您的帮助感谢您的帮助感谢您的帮助
'package:google_maps_flutter_platform_interface/src/types/camera.dart': Failed assertion: line 26 pos 16: 'target != null': is not true.
import 'dart:async';
import 'package:geolocator/geolocator.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class CreateMapMarker extends StatefulWidget {
@override
_CreateMapMarkerState createState() => _CreateMapMarkerState();
}
class _CreateMapMarkerState extends State<CreateMapMarker> {
@override
void initState(){
super.initState();
doSomeAsyncStuff();
}
Future<void> doSomeAsyncStuff() async {
Geolocator().getCurrentPosition().then((currloc){
setState((){
_center = LatLng(currloc.latitude,currloc.longitude);
});
});
}
Completer<GoogleMapController> _controller = Completer();
static LatLng _center;
bool _isChecked = true;
final Set<Marker> _markers = {};
void _onMapCreated(GoogleMapController controller) {
Geolocator().getCurrentPosition().then((currloc){
_center = LatLng(currloc.latitude,currloc.longitude);
});
_controller.complete(controller);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(child:Text('Create Marker')),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
markers: _markers,
onTap: _handleTap,
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
),
);
}
_handleTap(LatLng point) {
setState(() {
_markers.add(Marker(
markerId: MarkerId(point.toString()),
position: point,
infoWindow: InfoWindow(
title: 'I am a marker',
),
icon:
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueMagenta),
onTap:()=> showModalBottomSheet(
context: this.context,
builder: (builder){
return Container(
child:Center(
child: new Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Enter name',
),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Enter type',
),
),
CheckboxListTile(
title: Text("Active ?"),
value: _isChecked,
onChanged: (newValue) {
setState(() {
_isChecked = newValue;
});
},
controlAffinity: ListTileControlAffinity.leading, // <-- leading Checkbox
),
],
),
));
}
)
)
);
});
}
}
解决方案
您收到错误是因为最初 _center 将为 null,因为我们正在处理 Future(即异步操作)。此外,“initialCameraPosition”用于设置地图相机的初始位置。如果您想在创建地图后更新相机位置,则可以使用 GoogleMapController 实例的“moveCamera”方法。这主要用于更改地图相机位置。看看下面更新的代码:
class MapPage extends StatefulWidget {
MapPage({Key key}) : super(key: key);
@override
_MapPageState createState() => _MapPageState();
}
class _MapPageState extends State<MapPage> {
GoogleMapController _controller;
bool _isChecked = true;
final Set<Marker> _markers = {};
@override
void initState() {
super.initState();
doSomeAsyncStuff();
}
void _onMapCreated(GoogleMapController controller) {
_controller = controller;
}
Future<void> doSomeAsyncStuff() async {
Geolocator().getCurrentPosition().then((currloc) {
print("current location $currloc");
if (currloc != null) {
_controller.moveCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
bearing: 270.0,
target: LatLng(currloc.latitude, currloc.longitude),
tilt: 30.0,
zoom: 17.0,
),
),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text('Create Marker')),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
markers: _markers,
onTap: _handleTap,
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(0.0, 0.0),
zoom: 11.0,
),
),
);
}
_handleTap(LatLng point) {
setState(() {
_markers.add(Marker(
markerId: MarkerId(point.toString()),
position: point,
infoWindow: InfoWindow(
title: 'I am a marker',
),
icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueMagenta),
onTap: () => showModalBottomSheet(
context: this.context,
builder: (builder) {
return Container(
child: Center(
child: new Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
labelText: 'Enter name',
),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Enter type',
),
),
CheckboxListTile(
title: Text("Active ?"),
value: _isChecked,
onChanged: (newValue) {
setState(() {
_isChecked = newValue;
});
},
controlAffinity: ListTileControlAffinity
.leading, // <-- leading Checkbox
),
],
),
));
})));
});
}
}
推荐阅读
- java - 如何更改 dist 文件夹路径而不覆盖输出文件夹中的现有文件
- if-statement - 计算不断增加的列数,右侧有数据
- javascript - 按特定值对 JSON 进行排序,如果键值相同,则按排序键对键进行分组
- c# - 如何验证要从 excel 表中加载到数据库的数据(行)?
- c - 如何修复我的代码以读取函数中的字符串,该函数返回指向结构的指针
- python-3.x - ImportError:libarrow.so.14:无法打开共享对象文件:没有这样的文件或目录| Python
- typescript - 使用特殊方法减少类中的输入
- excel - Excel条件格式问题
- python-3.x - 如何刮价格
- html - Lucid“术语”类型的真正含义