0

我正在添加 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”的方法。

此错误在此代码中找到。

4

1 回答 1

0

没有addMarker方法GoogleMapController。要在地图上添加标记,您需要将Marker()对象列表传递给您的GoogleMaps()小部件。这是如何做到的:

您需要初始化一个列表,您将在其中存储所有Marker()对象:

 List<Marker> _markers = <Marker>[];

Marker()您可以使用在该列表中添加对象_markers.add()。例如,这可以在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[]您可以通过从列表中删除标记来删除它。

于 2021-11-10T05:28:41.420 回答