Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:user_location/user_location.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:latlong/latlong.dart';

void main() => runApp(MyApp());

Expand All @@ -21,10 +21,10 @@ class MyApp extends StatelessWidget {
}

class HomePage extends StatelessWidget {
final MapController mapController = MapController();
final List<Marker> markers = [];
final StreamController<LatLng> markerlocationStream = StreamController();
late UserLocationOptions userLocationOptions;
MapController mapController = MapController();
List<Marker> markers = [];
StreamController<LatLng> markerlocationStream = StreamController();
UserLocationOptions userLocationOptions;

onTapFAB() {
print('Callback function has been called');
Expand All @@ -42,7 +42,7 @@ class HomePage extends StatelessWidget {
context: context,
mapController: mapController,
markers: markers,
onLocationUpdate: (LatLng pos, double? speed) =>
onLocationUpdate: (LatLng pos, double speed) =>
print("onLocationUpdate ${pos.toString()}"),
updateMapLocationOnPositionChange: false,
showMoveToCurrentLocationFloatingActionButton: true,
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ publish_to: 'none'
version: 1.0.0

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.1.0 <3.0.0"

dependencies:
flutter:
Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data!.startsWith('Running on:'),
widget.data.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
67 changes: 33 additions & 34 deletions lib/src/user_location_layer.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import "dart:math" show pi;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_compass/flutter_compass.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:latlong2/latlong.dart';
import 'package:latlong/latlong.dart';
import 'package:location/location.dart';
import 'package:user_location/src/user_location_marker.dart';
import 'package:user_location/src/user_location_options.dart';
Expand All @@ -25,24 +26,24 @@ class MapsPluginLayer extends StatefulWidget {

class _MapsPluginLayerState extends State<MapsPluginLayer>
with TickerProviderStateMixin, WidgetsBindingObserver {
LatLng? _currentLocation;
UserLocationMarker? _locationMarker;
LatLng _currentLocation;
UserLocationMarker _locationMarker;
EventChannel _stream = EventChannel('locationStatusStream');
var location = Location();

late bool mapLoaded;
late bool initialStateOfupdateMapLocationOnPositionChange;
bool mapLoaded;
bool initialStateOfupdateMapLocationOnPositionChange;

double? _direction;
double _direction;

StreamSubscription<LocationData>? _onLocationChangedStreamSubscription;
StreamSubscription<CompassEvent>? _compassStreamSubscription;
StreamSubscription? _locationStatusChangeSubscription;
StreamSubscription<LocationData> _onLocationChangedStreamSubscription;
StreamSubscription<CompassEvent> _compassStreamSubscription;
StreamSubscription _locationStatusChangeSubscription;

@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
WidgetsBinding.instance.addObserver(this);

initialStateOfupdateMapLocationOnPositionChange =
widget.options.updateMapLocationOnPositionChange;
Expand All @@ -55,7 +56,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>

@override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
WidgetsBinding.instance.removeObserver(this);
_locationStatusChangeSubscription?.cancel();
_onLocationChangedStreamSubscription?.cancel();
_compassStreamSubscription?.cancel();
Expand Down Expand Up @@ -139,11 +140,11 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
if (onValue.latitude == null || onValue.longitude == null) {
_currentLocation = LatLng(0, 0);
} else {
_currentLocation = LatLng(onValue.latitude!, onValue.longitude!);
_currentLocation = LatLng(onValue.latitude, onValue.longitude);
}

var height = 20.0 * (1 - (onValue.accuracy! / 100));
var width = 20.0 * (1 - (onValue.accuracy! / 100));
var height = 20.0 * (1 - (onValue.accuracy / 100));
var width = 20.0 * (1 - (onValue.accuracy / 100));
if (height < 0 || width < 0) {
height = 20;
width = 20;
Expand All @@ -159,15 +160,15 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
height: 20.0,
width: 20.0,
point:
LatLng(_currentLocation!.latitude, _currentLocation!.longitude),
LatLng(_currentLocation.latitude, _currentLocation.longitude),
builder: (context) {
return Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
if (_direction != null && widget.options.showHeading)
ClipOval(
child: Transform.rotate(
angle: _direction! / 180 * math.pi,
angle: _direction / 180 * math.pi,
child: CustomPaint(
size: Size(60.0, 60.0),
painter: MyDirectionPainter(),
Expand All @@ -184,7 +185,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
width: 20,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue[300]!.withOpacity(0.7),
color: Colors.blue[300].withOpacity(0.7),
),
),
),
Expand Down Expand Up @@ -225,22 +226,20 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
setState(() {
mapLoaded = true;
});
animatedMapMove(_currentLocation!, widget.options.defaultZoom,
animatedMapMove(_currentLocation, widget.options.defaultZoom,
widget.options.mapController, this);
}
});
});
}
}

void _moveMapToCurrentLocation({double? zoom}) {
void _moveMapToCurrentLocation({double zoom}) {
if (_currentLocation != null) {
animatedMapMove(
LatLng(
_currentLocation!.latitude,
_currentLocation!.longitude,
),
zoom ?? widget.map.zoom,
LatLng(_currentLocation.latitude ?? LatLng(0, 0),
_currentLocation.longitude ?? LatLng(0, 0)),
zoom ?? widget.map.zoom ?? 15,
widget.options.mapController,
this,
);
Expand All @@ -249,7 +248,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>

void _handleLocationStatusChanges() {
printLog(_stream.toString());
bool? _locationStatusChanged;
bool _locationStatusChanged;
if (_locationStatusChanged == null) {
_locationStatusChangeSubscription =
_stream.receiveBroadcastStream().listen((onData) {
Expand All @@ -268,7 +267,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>

void _handleCompassDirection() {
if (widget.options.showHeading) {
_compassStreamSubscription = FlutterCompass.events!.listen((event) {
_compassStreamSubscription = FlutterCompass.events.listen((event) {
setState(() {
_direction = event.heading;
});
Expand All @@ -281,8 +280,8 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
if (widget.options.onLocationUpdate == null) {
printLog("Stream not provided");
} else {
widget.options.onLocationUpdate!(
LatLng(onValue.latitude!, onValue.longitude!),
widget.options.onLocationUpdate(
LatLng(onValue.latitude, onValue.longitude),
onValue.speed,
);
}
Expand Down Expand Up @@ -314,7 +313,7 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
initialize();
_moveMapToCurrentLocation(zoom: widget.options.defaultZoom);
if (widget.options.onTapFAB != null) {
widget.options.onTapFAB!();
widget.options.onTapFAB();
}
},
child: widget.options.moveToCurrentLocationFloatingActionButton ??
Expand Down Expand Up @@ -382,11 +381,11 @@ class _MapsPluginLayerState extends State<MapsPluginLayer>
}

void forceMapUpdate() {
var zoom = widget.options.mapController!.zoom;
widget.options.mapController!.move(widget.options.mapController!.center,
widget.options.mapController!.zoom + 0.000001);
widget.options.mapController!
.move(widget.options.mapController!.center, zoom);
var zoom = widget.options.mapController.zoom;
widget.options.mapController.move(widget.options.mapController.center,
widget.options.mapController.zoom + 0.000001);
widget.options.mapController
.move(widget.options.mapController.center, zoom);
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/user_location_marker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import 'package:flutter_map/flutter_map.dart';

class UserLocationMarker extends Marker {
UserLocationMarker({
required point,
required builder,
required width,
required height,
AnchorPos? anchorPos,
point,
builder,
width,
height,
AnchorPos anchorPos,
}) : super(
point: point,
builder: builder,
Expand Down
18 changes: 9 additions & 9 deletions lib/src/user_location_options.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:flutter/material.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:latlong2/latlong.dart';
import 'package:latlong/latlong.dart';

class UserLocationOptions extends LayerOptions {
BuildContext context;
List<Marker?> markers;
MapController? mapController;
List<Marker> markers;
MapController mapController;

Widget? markerWidget;
Widget markerWidget;
bool updateMapLocationOnPositionChange;
bool showMoveToCurrentLocationFloatingActionButton;
bool zoomToCurrentLocationOnLoad;
Widget? moveToCurrentLocationFloatingActionButton;
Widget moveToCurrentLocationFloatingActionButton;

void Function(LatLng location, double? speed)? onLocationUpdate;
Function()? onTapFAB;
void Function(LatLng location, double speed) onLocationUpdate;
Function() onTapFAB;

double fabBottom;
double fabRight;
Expand All @@ -37,8 +37,8 @@ class UserLocationOptions extends LayerOptions {
int locationUpdateIntervalMs;

UserLocationOptions(
{required this.context,
required this.markers,
{@required this.context,
@required this.markers,
this.mapController,
this.markerWidget,
this.onLocationUpdate,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ author: Gaurab Panthee <gaurab.panthee@gmail.com>
homepage: https://github.com/igaurab/UserLocationPlugin

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: ">=2.3.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
location: ^4.1.1
flutter_map: ^0.13.0
flutter_map: ^0.12.0
flutter_compass: ^0.6.0

dev_dependencies:
Expand Down