tlserver / flutter_map_location_marker

A flutter map plugin for displaying device current location.
https://pub.dev/packages/flutter_map_location_marker
BSD 3-Clause "New" or "Revised" License
97 stars 81 forks source link

The HeadingSector is too large and the width of the headingsector does not change when the accuracy changes. #72

Closed keskink closed 1 year ago

keskink commented 1 year ago

The HeadingSector is too large and the width of the headingsector does not change when the accuracy changes.

I set accuracy as like (heading!.accuracy * 0.4) in location_marker_layer.dart . Now much better. But it is fixed not changing. in my opinion, headingsector should change when accuracy changed. gps_1 gps2

tlserver commented 1 year ago

UPDATE:

"The problem is that flutter_compass (the default implementation) do not give any accuracy. " This is wrong. flutter_compass give accuracy also in degree but not rad. This bug will be fixed in next release.


I argee with you that HeadingSector should change when accuracy changed and this is the current behavior. The problem is that flutter_compass (the default implementation) do not give any accuracy. You can use your own way to get accuracy or just override the accuracy by some other value as you like, for example:

    const factory = LocationMarkerDataStreamFactory();
    _headingStream = factory.fromCompassHeadingStream(defAccuracy: pi * 0.12).asBroadcastStream();

Also try this example:

import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_joystick/flutter_joystick.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:latlong2/latlong.dart';

// A demo for a custom position and heading stream. In this example, the
// location marker is controlled by a joystick instead of the device sensor.
// This example provide same behavior as Joystick Example.
class CustomStreamExample extends StatefulWidget {
  @override
  _CustomStreamExampleState createState() => _CustomStreamExampleState();
}

class _CustomStreamExampleState extends State<CustomStreamExample> {
  late final StreamController<LocationMarkerPosition> _positionStreamController;
  late final StreamController<LocationMarkerHeading> _headingStreamController;
  double _currentLat = 0;
  double _currentLng = 0;

  @override
  void initState() {
    super.initState();
    _positionStreamController = StreamController()
      ..add(
        LocationMarkerPosition(
          latitude: _currentLat,
          longitude: _currentLng,
          accuracy: 0,
        ),
      );
    _headingStreamController = StreamController()
      ..add(
        LocationMarkerHeading(
          heading: 0,
          accuracy: pi * 0.2,
        ),
      );
  }

  @override
  void dispose() {
    _positionStreamController.close();
    _headingStreamController.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Custom Stream Example'),
      ),
      body: Stack(
        children: [
          FlutterMap(
            options: MapOptions(
              center: const LatLng(0, 0),
              zoom: 1,
              minZoom: 0,
              maxZoom: 19,
            ),
            // ignore: sort_child_properties_last
            children: [
              TileLayer(
                urlTemplate:
                    'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                subdomains: const ['a', 'b', 'c'],
                userAgentPackageName:
                    'net.tlserver6y.flutter_map_location_marker.example',
                maxZoom: 19,
              ),
              CurrentLocationLayer(
                positionStream: _positionStreamController.stream,
                headingStream: _headingStreamController.stream,
              ),
            ],
          ),
          Positioned(
            right: 20,
            bottom: 20,
            child: Joystick(
              listener: (details) {
                _currentLat -= details.y;
                _currentLat = _currentLat.clamp(-85, 85);
                _currentLng += details.x;
                _currentLng = _currentLng.clamp(-180, 180);
                _positionStreamController.add(
                  LocationMarkerPosition(
                    latitude: _currentLat,
                    longitude: _currentLng,
                    accuracy: 0,
                  ),
                );
                if (details.x != 0 || details.y != 0) {
                  _headingStreamController.add(
                    LocationMarkerHeading(
                      heading:
                          (atan2(details.y, details.x) + pi * 0.5) % (pi * 2),
                      accuracy: pi * (0.2 + Random().nextDouble() * 0.1),
                    ),
                  );
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}