Обновления местоположения обновляют только маркер, но не карту. флаттер - PullRequest
0 голосов
/ 15 февраля 2020

Я получаю поток 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 ]

1 голос
/ 15 февраля 2020

Существует 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;
        },
      ),
    );
  }
}
0 голосов
/ 16 февраля 2020

Я нашел ошибку. _controller инициализируется внутри setMap(), а не внутри initState(), поэтому, когда я вызываю _controller.move(LatLng(_position.latitude, _position.longitude), 16); внутри startTracking(), он выдавал ошибку, не имея связи с картой ... и не перемещал карту. Инициализация внутри initState() работает как положено. Большое спасибо

...