位置更新仅更新标记,而不更新地图。扑

时间:2020-02-15 08:22:38

标签: flutter flutter-dependencies

我收到的是Positions流,然后更新一个_position变量,该变量在center:中用于设置地图位置,在Marker中用于显示用户位置。 问题在于标记会更新,但地图位置不会更新。 预期的行为是,当地图在其下方移动时,标记始终保持在屏幕中央,但是情况恰恰相反。你能看到我把事情弄错了吗? 非常感谢您的时间和帮助。

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),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:1)

GoogleMapController个带有moveCamera / animateCamera方法。他们接受CameraUpdate参数和CameraPosition作为新目标。

class LocationLabPage extends StatefulWidget {
  @override
  _LocationLabPageState createState() => _LocationLabPageState();
}

class _LocationLabPageState extends State<LocationLabPage> {
  GoogleMapController _mapController;
  StreamSubscription _streamSubscription;

  @override
  void initState() {
    _streamSubscription = LocationService().locationStream.listen((event) {
      _mapController.moveCamera(CameraUpdate.newLatLng(LatLng(event.latitude, event.longitude)));
    });
    super.initState();
  }

  @override
  void dispose() {
    _streamSubscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: GoogleMap(
        myLocationEnabled: true,
        myLocationButtonEnabled: true,
        initialCameraPosition: CameraPosition(target: LatLng(37.7, -122.2), zoom: 9.0),
        onMapCreated: (GoogleMapController controller) {
          _mapController = controller;
        },
      ),
    );
  }
}

答案 1 :(得分:0)

我发现了错误。 _controller是在setMap()内部初始化的,而不是在initState()内部初始化的,所以当我在_controller.move(LatLng(_position.latitude, _position.longitude), 16);内部调用startTracking()时,抛出了与地图没有连接的错误。并没有移动地图。 将其初始化在initState()中可以正常工作。 非常感谢