Flutter 地图标记不显示
Flutter Map markers not displaying
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
class NewMap extends StatefulWidget {
@override
_NewMapState createState() => _NewMapState();
}
class _NewMapState extends State<NewMap> {
GoogleMapController _controller;
Position position;
Widget _child;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
BitmapDescriptor pinLocationIcon;
@override
void initState() {
_child = SpinKitRipple(
itemBuilder: (BuildContext context, int index) {
return DecoratedBox(
decoration: BoxDecoration(
color: index.isEven ? Colors.grey : Color(0xffffb838),
),
);
},
);
getCurrentLocation();
populateClients();
setCustomMapPin();
super.initState();
}
void getCurrentLocation() async {
Position res = await getCurrentPosition();
setState(() {
position = res;
_child = mapWidget();
});
}
populateClients() {
FirebaseFirestore.instance.collection('map').get().then((docs) {
if (docs.docs.isNotEmpty) {
for (int i = 0; i < docs.docs.length; ++i) {
initMarker(docs.docs[i].data(), docs.docs[i].id);
}
}
});
}
void initMarker(tomb, tombId) {
var markerIdVal = tombId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(tomb['location'].latitude, tomb['location'].longitude),
icon: pinLocationIcon,
);
setState(() {
markers[markerId] = marker;
});
}
void setCustomMapPin() async {
pinLocationIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5), 'assets/images/pin.png');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Color(0xffffb838)),
centerTitle: true,
backgroundColor: Colors.white,
),
body: _child,
);
}
Widget mapWidget() {
return Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 10,
),
markers: Set<Marker>.of(markers.values),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
},
compassEnabled: true,
myLocationEnabled: true,
),
SizedBox(
height: 26,
),
],
);
}
}
这是我尝试在控制台上打印经纬度值时的代码,以便从 firestore 读取这些值。
地图正确显示当前位置也正在显示。
据我所知,标记已创建,因为我可以将经纬度打印到控制台
但是地图上没有显示标记。
我该如何解决这个问题
诀窍在于 IDE,感谢 Muthu Thavamani,我能够确定代码中没有错误。
由于应用程序的构建而发生,删除它后问题正在解决
尝试
flutter clean
IDE 终端中的命令
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
class NewMap extends StatefulWidget {
@override
_NewMapState createState() => _NewMapState();
}
class _NewMapState extends State<NewMap> {
GoogleMapController _controller;
Position position;
Widget _child;
Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
BitmapDescriptor pinLocationIcon;
@override
void initState() {
_child = SpinKitRipple(
itemBuilder: (BuildContext context, int index) {
return DecoratedBox(
decoration: BoxDecoration(
color: index.isEven ? Colors.grey : Color(0xffffb838),
),
);
},
);
getCurrentLocation();
populateClients();
setCustomMapPin();
super.initState();
}
void getCurrentLocation() async {
Position res = await getCurrentPosition();
setState(() {
position = res;
_child = mapWidget();
});
}
populateClients() {
FirebaseFirestore.instance.collection('map').get().then((docs) {
if (docs.docs.isNotEmpty) {
for (int i = 0; i < docs.docs.length; ++i) {
initMarker(docs.docs[i].data(), docs.docs[i].id);
}
}
});
}
void initMarker(tomb, tombId) {
var markerIdVal = tombId;
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(tomb['location'].latitude, tomb['location'].longitude),
icon: pinLocationIcon,
);
setState(() {
markers[markerId] = marker;
});
}
void setCustomMapPin() async {
pinLocationIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5), 'assets/images/pin.png');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Color(0xffffb838)),
centerTitle: true,
backgroundColor: Colors.white,
),
body: _child,
);
}
Widget mapWidget() {
return Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 10,
),
markers: Set<Marker>.of(markers.values),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
},
compassEnabled: true,
myLocationEnabled: true,
),
SizedBox(
height: 26,
),
],
);
}
}
这是我尝试在控制台上打印经纬度值时的代码,以便从 firestore 读取这些值。 地图正确显示当前位置也正在显示。 据我所知,标记已创建,因为我可以将经纬度打印到控制台 但是地图上没有显示标记。
我该如何解决这个问题
诀窍在于 IDE,感谢 Muthu Thavamani,我能够确定代码中没有错误。
由于应用程序的构建而发生,删除它后问题正在解决
尝试
flutter clean
IDE 终端中的命令