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..