How to create an geofencing application using dart(flutter)?

1,937 views
Skip to first unread message

Spandan Bhandari

unread,
Oct 14, 2019, 3:50:03 AM10/14/19
to Flutter Development (flutter-dev)
I want to buid an geofencing application in flutter. But I am not finding a way to create geofence from dart so I'm doing platform-channels from java->dart. Is there any other way to create geofence in flutter. Most of the plugins of geofence in flutter does not work properly. So I want to create a geofence application from scratch. Please give me some reference regrading it any tutorials, codes samples, etc. Thank you
Message has been deleted

Surya Nigrat Datunsolang

unread,
Oct 14, 2019, 9:48:36 AM10/14/19
to Flutter Development (flutter-dev)
Message has been deleted

Surya Nigrat Datunsolang

unread,
Oct 15, 2019, 8:00:32 AM10/15/19
to Flutter Development (flutter-dev)
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:simtaru_app/provider/layerprovider.dart';

class LayerModel {
  bool _isLoaded;
  String _layerName;
  List<List<LatLng>> _polyPoints;

  String get layerName => _layerName;
  List<List<LatLng>> get polyPoints => _polyPoints;

  LayerModel(String layerName, List<List<LatLng>> polyPoints) {
    _layerName = layerName;
    _polyPoints = polyPoints;
    _isLoaded = false;
  }
  bool get isLoaded => _isLoaded;
  void setBoolLoad(bool val) {
    _isLoaded = val;
  }

  void loadToMap(bool val, LayerProvider model) {
    if (val) {
      _isLoaded = true;
      model.loadPolyFromLayers(layerName: _layerName);
    } else {
      model.removeFormMap(layerName);
      _isLoaded = false;
    }
  }

  bool isInside(Position pos) {
    List<String> isInside = [];
    polyPoints.forEach((point) {
      // var points = polygon
      //     .points; //just for testing... TODO: buat algoritma untuk mengecek semua polygon.
      int count = 0;
      while (count < (point.length - 1)) {
        var currentPointLat = point[count].latitude;
        var currentPointLng = point[count].longitude;

        var nextPointLat = point[count + 1].latitude;
        var nextPointLng = point[count + 1].longitude;

        bool isIntersect = ((currentPointLng > pos.longitude) !=
                (nextPointLng > pos.longitude)) &&
            (pos.latitude <
                (nextPointLat - currentPointLat) *
                        (pos.longitude - currentPointLng) /
                        (nextPointLng - currentPointLng) +
                    currentPointLat);

        isInside.add(isIntersect.toString());
        count++;
      }
    });
    var trueCount = 0;
    isInside.forEach((f) {
      if (f == 'true') {
        trueCount++;
      }
    });
    return trueCount.isOdd;
  }
}




Pay attention to the method 'bool isInside' method.
the _polyPoints parameter defines a list of polygons' coordinates which is used to test user's position against. 

The idea of Ray Casting algorithm is that if you draw a line at any direction from a point, the number of times the line intersects the boundary of an area tells whether the point is inside or outside of that area. If the number of intersection is zero or even number then it is outside. If the number is odd then it is inside of the area..


On Monday, October 14, 2019 at 3:50:03 PM UTC+8, Spandan Bhandari wrote:

Surya Nigrat Datunsolang

unread,
Oct 15, 2019, 8:09:18 AM10/15/19
to Flutter Development (flutter-dev)
also, the (Position pos) param of the isInside method defines the user Position (which is provided by Geolocator)


On Monday, October 14, 2019 at 3:50:03 PM UTC+8, Spandan Bhandari wrote:
Reply all
Reply to author
Forward
0 new messages