我正在添加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"定义getter"clearMarkers"。请尝试导入定义"clearMarkers"的库,将名称更正为现有getter的名称,或定义名为"clearMarker"的getter或字段。方法"MarkerOptions"不是为类型"FireMapState"定义的。请尝试将名称更正成现有方法的名称,或定义名为"MarkerOptions"的方法。方法"InfoWindowText"不是为类型"FireMapState"定义的。请尝试将名称更正为现有方法的名称,或定义名为"InfoWindowText"的方法。
此错误在此代码中找到
GoogleMapController
没有addMarker
方法。要在地图上添加标记,您需要将Marker()
对象列表传递给GoogleMaps()
小部件。以下是操作方法:
您需要初始化一个列表,在其中存储所有Marker()
对象:
List<Marker> _markers = <Marker>[];
可以使用_markers.add()
将Marker()
对象添加到该列表中。例如,这可以在initState()
方法中完成:
@override
void initState() {
// TODO: implement initState
super.initState();
_markers.add(
Marker(
markerId: MarkerId('markerId'),
position: LatLng(37.421946, -122.084090),
),
);
}
最后,您可以将Marker()
对象列表添加到GoogleMap()
小部件中:
GoogleMap(
mapType: MapType.normal,
markers: Set.from(_markers), /* <---- here is how to add the list of markers */
initialCameraPosition: _initialCameraPosition,
onMapCreated: _onMapCreated,
),
您可以通过将标记从_markers[]
列表中删除来删除标记。