没有为“GoogleMapController”类型定义方法“addMarker”
Posted
技术标签:
【中文标题】没有为“GoogleMapController”类型定义方法“addMarker”【英文标题】:The method 'addMarker' isn't defined for the type 'GoogleMapController' 【发布时间】:2021-12-22 09:00:49 【问题描述】:我正在添加 removeMarker 和 addMarker,但它显示 - “方法 'addMarker' 没有为类型 'GoogleMapController' 定义。尝试将名称更正为现有方法的名称,或定义一个名为 '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.valuekm',
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”的方法。
在这段代码中发现了这个错误。
【问题讨论】:
【参考方案1】: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[]
列表中删除标记来删除它。
【讨论】:
以上是关于没有为“GoogleMapController”类型定义方法“addMarker”的主要内容,如果未能解决你的问题,请参考以下文章