我正在添加 removeMarker 和 addMarker,但它显示 - “没有为类型 'GoogleMapController' 定义方法 'addMarker'。尝试将名称更正为现有方法的名称,或定义一个名为 'addMarker' 的方法。”
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: FireMap(),
)
);
}
}
class FireMap extends StatefulWidget {
const FireMap({Key? key}) : super(key: key);
@override
State createState() => FireMapState();
}
class FireMapState extends State<FireMap> {
late GoogleMapController mapController;
Location location = Location();
//Firestore firestore = Firestore.instance;
FirebaseFirestore firestore = FirebaseFirestore.instance;
Geoflutterfire geo = Geoflutterfire();
// Stateful Data
BehaviorSubject<double> radius = BehaviorSubject();
late Stream<dynamic>query;
// Subscription
late StreamSubscription subscription;
build(context) {
return Stack(children: [
GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(24.142, -110.321),
zoom: 15
),
onMapCreated: _onMapCreated,
myLocationEnabled: true,
mapType: MapType.hybrid,
compassEnabled: true,
onCameraMove: _animateToUser(),
),
Positioned(
bottom: 50,
right: 10,
child:
FlatButton(
child: const Icon(Icons.pin_drop, color: Colors.white),
color: Colors.green,
onPressed: _addGeoPoint
)
),
Positioned(
bottom: 50,
left: 10,
child: Slider(
min: 100.0,
max: 500.0,
divisions: 4,
value: radius.value,
label: 'Radius ${radius.value}km',
activeColor: Colors.green,
inactiveColor: Colors.green.withOpacity(0.2),
onChanged: _updateQuery,
)
)
]);
}
_onMapCreated(GoogleMapController controller) {
_startQuery();
setState(() {
mapController = controller;
});
}
addMarker() {
final Marker marker = Marker(
markerId: MarkerId,
position: mapController.cameraPosition.target,
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow.noText,
//infoWindowText: const InfoWindowText('Magic Marker', '')
);
mapController.addMarker();
}
_animateToUser() async {
var pos = await location.getLocation();
mapController.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(pos['latitude'], pos['longitude']),
zoom: 17.0,
)
)
);
}
Future<DocumentReference> _addGeoPoint() async {
var pos = await location.getLocation();
GeoFirePoint point = geo.point(latitude: pos['latitude'], longitude: pos['longitude']);
return firestore.collection('locations').add({
'position': point.data,
'name': 'Yay I can be queried!'
});
}
void _updateMarkers(List<DocumentSnapshot> documentList) {
print(documentList);
mapController.clearMarkers;
for (var document in documentList) {
GeoPoint pos = document.data['position']['geopoint'];
double distance = document.data['distance'];
var marker = MarkerOptions(
position: LatLng(pos.latitude, pos.longitude),
icon: BitmapDescriptor.defaultMarker,
infoWindowText: InfoWindowText('Magic Marker', '$distance kilometers from query center')
);
mapController.addMarker(marker);
}
}
_startQuery() async {
// Get users location
var pos = await location.getLocation();
double lat = pos['latitude'];
double lng = pos['longitude'];
var ref = firestore.collection('locations');
GeoFirePoint center = geo.point(latitude: lat, longitude: lng);
subscription = radius.switchMap((rad) {
return geo.collection(collectionRef: ref).within(
center: center,
radius: rad,
field: 'position',
strictMode: true
);
}).listen(_updateMarkers);
}
_updateQuery(value) {
final zoomMap = {
100.0: 12.0,
200.0: 10.0,
300.0: 7.0,
400.0: 6.0,
500.0: 5.0
};
final zoom = zoomMap[value];
mapController.moveCamera(CameraUpdate.zoomTo(zoom));
setState(() {
radius.add(value);
});
}
@override
dispose() {
subscription.cancel();
super.dispose();
}
}
没有为“GoogleMapController”类型定义吸气剂“clearMarkers”。尝试导入定义“clearMarkers”的库,将名称更正为现有 getter 的名称,或定义名为“clearMarkers”的 getter 或字段。没有为“FireMapState”类型定义“MarkerOptions”方法。尝试更正name 为现有方法的名称,或定义名为“MarkerOptions”的方法。方法“InfoWindowText”未为“FireMapState”类型定义。尝试将名称更正为现有方法的名称,或定义名为“InfoWindowText”的方法。
此错误在此代码中找到。