Flutter 位置更新仅更新标记,而不更新地图。颤振

Flutter 位置更新仅更新标记,而不更新地图。颤振,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我收到一个位置流,然后更新一个_position变量,该变量既用于设置地图位置,也用于显示用户位置的标记。 问题是标记会更新,但地图位置不会更新。 预期的行为是,当地图在其下方移动时,标记在屏幕中心保持稳定,但情况正好相反。你能看出我哪里搞错了吗? 非常感谢您的时间和帮助 class HomeScreen extends StatefulWidget { final String name; HomeScreen({ Key key, @required this.na

我收到一个位置流,然后更新一个_position变量,该变量既用于设置地图位置,也用于显示用户位置的标记。 问题是标记会更新,但地图位置不会更新。 预期的行为是,当地图在其下方移动时,标记在屏幕中心保持稳定,但情况正好相反。你能看出我哪里搞错了吗? 非常感谢您的时间和帮助

class HomeScreen extends StatefulWidget {
  final String name;

  HomeScreen({
    Key key,
    @required this.name,
  }) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  Geolocator _geolocator;
  Position _position;

  FlutterMap _flutterMap;
  MapOptions _mapOptions;
  MapController _controller;
  void checkPermission() {
    _geolocator.checkGeolocationPermissionStatus().then((status) {
      print('status: $status');
    });
    _geolocator
        .checkGeolocationPermissionStatus(
            locationPermission: GeolocationPermission.locationAlways)
        .then((status) {
      print('always status: $status');
    });
    _geolocator.checkGeolocationPermissionStatus(
        locationPermission: GeolocationPermission.locationWhenInUse)
      ..then((status) {
        print('whenInUse status: $status');
      });
  }

  @override
  void initState() {
    super.initState();
    _geolocator = Geolocator();
    checkPermission();
    updateLocation();
  }

  void setMap() {
    _controller = MapController();
    _mapOptions = MapOptions(
      center: LatLng(_position.latitude, _position.longitude),
      minZoom: 16.0,
      maxZoom: 19.0,
    );
    _flutterMap = FlutterMap(
      options: _mapOptions,
      mapController: _controller,
      layers: [
//        PolygonLayer(polygonOpts, map, stream)
//                    PolygonLayerOptions(
//                      polygons:
//                    ),
        TileLayerOptions(
//        urlTemplate:'https://api.openrouteservice.org/mapsurfer/{z}/{x}/{y}.png?api_key=5b3ce3597851110001cf62484c4b65d85bc844eca3a2c6b9f300ddf4',
            urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
            subdomains: ['a', 'b', 'c'],
            keepBuffer: 20),
        new MarkerLayerOptions(
          markers: [
            Marker(
              point: LatLng(_position.latitude, _position.longitude),
              height: 200,
              width: 200,
              builder: (context) => IconButton(
                icon: Icon(Icons.location_on),
                color: Colors.red,
                iconSize: 60,
                onPressed: () {
                  print('icon tapped');
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  void startTracking() {
    LocationOptions locationOptions =
        LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 0);

    StreamSubscription positionStream = _geolocator
        .getPositionStream(locationOptions)
        .listen((Position position) {
      setState(() {
        _position = position;
        if (_position != null) {
//          _controller.move(LatLng(_position.latitude, _position.longitude), 16);
          setMap();
        }
      });
      print(
          _position.latitude.toString() + ',' + _position.longitude.toString());
    });
  }

  void updateLocation() async {
    try {
      Position newPosition = await _geolocator
          .getCurrentPosition(
            desiredAccuracy: LocationAccuracy.bestForNavigation,
          )
          .timeout(new Duration(seconds: 5));
      print(
          'updateLocation(): ${newPosition.latitude.toString() + ',' + newPosition.longitude.toString()}');

      setState(() {
        _position = newPosition;
        setMap();
      });
    } catch (error) {
      print('Error updating location: ${error.toString()}');
    }
  }

  void postRequest() async {
    print('postRequest called');

    // working properly
//    final request =
//    'http://router.project-osrm.org/trip/v1/driving/44.5018645003438,11.340018709036542;44.501342559233315,11.336182647230146?exclude=motorway';
//    final request = 'http://router.project-osrm.org/trip/v1/driving/44.5018645003438,11.340018709036542;44.501342559233315,11.33618264723014?source=first&destination=last&roundtrip=false&exclude=motorway&generate_hints=false&geometries=geojson&overview=full';
//    final request =
//        'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=44.5018645003438&flon=11.340018709036542&tlat=44.501342559233315&tlon=11.336182647230146&geometry=1&instructions=1&lang=it';

    // GOOOD
    final request =
        'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&v=bicycle&fast=0&layer=mapnik&flat=44.5018645003438&flon=11.340018709036542&tlat=44.502138&tlon=11.340402&geometry=1&instructions=1&lang=it';

    // only driving
//    final request =
//        'http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&flat=52.215676&flon=5.963946&tlat=52.2573&tlon=6.1799&v=motorcar&fast=1&layer=mapnik';

//    final request =
//        'https://routing.openstreetmap.de/routed-foot/route/v1/driving/13.7807085453224,51.0536161;13.7835429,51.0515885?overview=false&geometries=polyline&steps=true';

    // Await the http get response, then decode the json-formatted response.
    var response = await get(request);
    if (response.statusCode == 200) {
      var jsonResponse = convert.jsonDecode(response.body);
      print('${jsonResponse.runtimeType} : $jsonResponse');
      List<dynamic> coordinates = jsonResponse['coordinates'];
      print('coordinates are : $coordinates');
      print('coordinates are: ${coordinates.length}');
      Map<String, dynamic> properties = jsonResponse['properties'];
//      print('properties are $properties');
      String distance = properties['distance'];
      print('Route is $distance Km long.');
      String instructions = properties['description'];
      print('instructions are $instructions');

      List<LatLng> suggestedRoute = [];
      for (int i = 0; i < (coordinates.length); i++) {
        dynamic coordinate = coordinates[i];
        LatLng position = LatLng(coordinate[1], coordinate[0]);
        suggestedRoute.add(position);
        print('position is $position');
        print(i);
      }
      print('suggestedRoute is $suggestedRoute');
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Text(
          'Home',
          style: TextStyle(color: Colors.orangeAccent, fontSize: 40),
        ),
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.exit_to_app,
              color: Colors.orange,
              size: 35,
            ),
            onPressed: () {
              BlocProvider.of<AuthenticationBloc>(context).add(
                LoggedOut(),
              );
            },
          ),
        ],
      ),
      backgroundColor: Colors.white,
      body: SafeArea(
        minimum: EdgeInsets.symmetric(horizontal: 20),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                height: 670,
                width: 370,
                child: _flutterMap,

              ),
              SizedBox(
                height: 10,
              ),
              RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(5)),
                onPressed: () {
                  startTracking();
//                  setState(() {
////                    updateLocation();
//                  });
//
                },
                color: Colors.red,
                child: Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Text(
                    'alert',
                    style: TextStyle(color: Colors.white, fontSize: 30),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
有一个带有/methods的GoogleMapController。它们接受CameraUpdate参数作为新目标

类LocationLabPage扩展StatefulWidget{ @凌驾 _LocationLabPageState createState=>\u LocationLabPageState; } 类_LocationLabPageState扩展状态{ 谷歌地图控制器(GoogleMapController);; StreamSubscription\u StreamSubscription; @凌驾 无效初始状态{ _streamSubscription=LocationService.locationStream.listenevent{ _mapController.moveCameraCameraUpdate.NewLatlngLangeEvent.latitude、event.latitude; }; super.initState; } @凌驾 无效处置{ _streamSubscription.cancel; 超级处理; } @凌驾 小部件构建上下文上下文{ 返回脚手架 appBar:appBar, 正文:谷歌地图 myLocationEnabled:对, myLocationButtonEnabled:正确, 初始CameraPosition:CameraPositiontarget:LatLng37.7,-122.2,缩放:9.0, onMapCreated:GoogleMapController控制器{ _mapController=控制器; }, , ; } } 我发现了错误。 _控制器在setMap内部初始化,而不是在initState内部初始化,所以当我调用_controller.moveLatLng_position.latitude,_position.longitude,16;在startTracking内部,它抛出了一个错误,没有连接到地图..并且没有移动地图。 在initState内部对其进行初始化,效果如预期。
非常感谢

您好,谢谢您的回答。我使用的不是谷歌地图,而是同样具有移动功能的Flatter_地图包。我刚刚开始学习颤振,我想我仍然需要掌握状态的概念。当我将_position设置为set state中的新坐标时,不应该使用该变量更新所有事物的状态,因此标记和地图中心?@pskink我只是从flift开始,我猜我仍然必须掌握状态概念。当我将_position设置为set state中的新坐标时,不应该使用该变量更新所有对象的状态,因此标记和地图中心?