Skip to content

Commit

Permalink
Merge pull request #3 from tstefanich/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
bmoren committed Jun 26, 2016
2 parents f8f1903 + 704b2bc commit cec29ee
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 17 deletions.
92 changes: 86 additions & 6 deletions p5.geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,43 @@ p5.prototype.calcGeoDistance = function(lat1, lon1, lat2, lon2, units) {


/**
* Create a new geofence
* Calculate if a Location is inside Polygon
*
*
* @method watchPosition
* @param {float} Array of Objects with lat: and lon:
* @param {float} Object with lat and long of my location
* @return {boolean} true if geolocation is within polygon
*/

// http://jsfromhell.com/math/is-point-in-poly
// Adapted from: [http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html]
// Used Under MIT License
p5.prototype.isLocationInPolygon = function(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].lon <= pt.lon && pt.lon < poly[j].lon) || (poly[j].lon <= pt.lon && pt.lon < poly[i].lon))
&& (pt.lat < (poly[j].lat - poly[i].lat) * (pt.lon - poly[i].lon) / (poly[j].lon - poly[i].lon) + poly[i].lat)
&& (c = !c);
return c;
}



/**
* Create a new geoFenceCircle
*
* Watches the users current position and checks to see if they are witihn a set radius of a specified point.
*
* @method watchPosition
* @param {float} latitude of the first point
* @param {float} longitude of the first point
* @param {float} distance from the point to trigger the insideCallback
* @param {function} a callback to fire when the user is inside the geoFence
* @param {function} a callback to fire when the user is outside the geoFence
* @param {function} a callback to fire when the user is inside the geoFenceCircle
* @param {function} a callback to fire when the user is outside the geoFenceCircle
* @param {string} units to use: 'km' or 'mi', 'mi' is default if left blank
* @param {object} an positionOptions object: enableHighAccuracy, maximumAge, timeout
*/
p5.prototype.geoFence = function(lat, lon, fence, insideCallback, outsideCallback, units, options){
p5.prototype.geoFenceCircle = function(lat, lon, fence, insideCallback, outsideCallback, units, options){

this.lat = lat;
this.lon = lon;
Expand All @@ -225,7 +248,7 @@ p5.prototype.geoFence = function(lat, lon, fence, insideCallback, outsideCallbac
this.options = options;

this.geoError = function(message){
console.log("geoFence Error :" + message);
console.log("geoFenceCircle Error :" + message);
}

this.success = function(position){
Expand All @@ -241,7 +264,64 @@ p5.prototype.geoFence = function(lat, lon, fence, insideCallback, outsideCallbac
}

if (navigator.geolocation) {
// bind the callbacks to the geoFence 'this' so we can access, this.lat, this.lon, etc..
// bind the callbacks to the geoFenceCircle 'this' so we can access, this.lat, this.lon, etc..
navigator.geolocation.watchPosition(this.success.bind(this), this.geoError.bind(this), this.options);
}else{
geoError("geolocation not available");
};
}





/**
* Create a new geoFencePolygon
*
* Watches the users current position and checks to see if they are witihn a set radius of a specified point.
*
* @method watchPosition
* @param {float} latitude of the first point
* @param {float} longitude of the first point
* @param {float} distance from the point to trigger the insideCallback
* @param {function} a callback to fire when the user is inside the geoFenceCircle
* @param {function} a callback to fire when the user is outside the geoFenceCircle
* @param {string} units to use: 'km' or 'mi', 'mi' is default if left blank
* @param {object} an positionOptions object: enableHighAccuracy, maximumAge, timeout
*/


/*var points = [
{x: 34.076089, y: -118.440915},
{x: 34.076095, y: -118.440605},
{x: 34.075906, y: -118.440597},
{x: 34.075891, y: -118.440932},
];*/
p5.prototype.geoFencePolygon = function( ArrayOfObjectsWithLatLong, insideCallback, outsideCallback, units, options){

this.ArrayOfObjectsWithLatLong = ArrayOfObjectsWithLatLong;
this.units = units; //this should work since calcGeoDistance defaults to miles.
this.insideCallback = insideCallback;
this.outsideCallback = outsideCallback;
this.insideFence = false;
this.options = options;

this.geoError = function(message){
console.log("geoFencePolygon Error :" + message);
}

this.success = function(position){
this.insideFence = isLocationInPolygon(this.ArrayOfObjectsWithLatLong, { lat:position.coords.latitude, lon: position.coords.longitude });

if(this.insideFence == true){
if(typeof this.insideCallback == 'function'){ this.insideCallback(position.coords) };
}else{
if(typeof this.outsideCallback == 'function'){ this.outsideCallback(position.coords) };
}
}

if (navigator.geolocation) {
// bind the callbacks to the geoFenceCircle 'this' so we can access, this.lat, this.lon, etc..
navigator.geolocation.watchPosition(this.success.bind(this), this.geoError.bind(this), this.options);
}else{
geoError("geolocation not available");
Expand Down
83 changes: 72 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This activity is made possible by a research & planning grant from [Forecast Pub
+ [intervalCurrentPosition()](#intervalcurrentposition-used-with-a-callback)
+ [clearIntervalPos()](#clearintervalpos)
+ [calcGeoDistance()](#calcgeodistance)
+ [geoFence()](#geofence)
+ [geoFenceCircle()](#geoFenceCircle)

##### Useful Tips
+ When using the p5.js editor, you must 'run in browser' to emulate or receive location events.
Expand Down Expand Up @@ -137,22 +137,22 @@ function setup(){
print(distance);
}
```
#### geoFence()
###### geoFence(latitude, longitude, fenceDistance, insideCallback, outsideCallback, units, options)
geoFence() is class which creates a geofence around the provided lat/long point with a provided radius in provided units('mi' is default). It will fire a callback with an object containing position data when the user is inside of the geofence each time the location updates. It will fire a second callback each time the position updates and the user is outside of the geofence. Takes an optional object containing options for accuracy, timeout and age.
#### geoFenceCircle()
###### geoFenceCircle(latitude, longitude, fenceDistance, insideCallback, outsideCallback, units, options)
geoFenceCircle() is class which creates a geoFenceCircle around the provided lat/long point with a provided radius in provided units('mi' is default). It will fire a callback with an object containing position data when the user is inside of the geoFenceCircle each time the location updates. It will fire a second callback each time the position updates and the user is outside of the geoFenceCircle. Takes an optional object containing options for accuracy, timeout and age.
```javascript
var fence;
function setup(){

//optional options object for geoFence
//fence = new geoFence(44.979779, -93.325499, .05, insideTheFence, 'mi', fenceOptions)
//optional options object for geoFenceCircle
//fence = new geoFenceCircle(44.979779, -93.325499, .05, insideTheFence, 'mi', fenceOptions)
// fenceOptions = {
// enableHighAccuracy: false,
// timeout: 5000,
// maximumAge: 0
// };

fence = new geoFence(44.979779, -93.325499, 0.05, insideTheFence, outsideTheFence, 'mi')
fence = new geoFenceCircle(44.979779, -93.325499, 0.05, insideTheFence, outsideTheFence, 'mi')
}

function insideTheFence(position){
Expand All @@ -167,16 +167,77 @@ function outsideTheFence(position){
print("user is outside of the fence")
}
```
#### geoFence() insideFence boolean
###### geoFence(latitude, longitude, fenceDistance, insideCallback, outsideCallback, units, options)
geofence has a useful parameter for checking the fence status. .insideFence when called on your geofence object will return true or false depending on the users relationship to the fence.
#### geoFenceCircle() insideFence boolean
###### geoFenceCircle(latitude, longitude, fenceDistance, insideCallback, outsideCallback, units, options)
geoFenceCircle has a useful parameter for checking the fence status. .insideFence when called on your geoFenceCircle object will return true or false depending on the users relationship to the fence.
```javascript
var fence;
function setup(){
fence = new geoFence(44.979779, -93.325499, 0.05)
fence = new geoFenceCircle(44.979779, -93.325499, 0.05)
}

function draw(){
print(fence.insideFence);
}
```



#### geoFencePolygon()
###### geoFencePolygon(ArrayOfObjectsWithLatLong, insideCallback, outsideCallback, units, options)
geoFencePolygon() is class which creates a geoFencePolygon around the provided array of object that contain lat/long points. It will fire a callback with an object containing position data when the user is inside of the geoFencePolygon each time the location updates. It will fire a second callback each time the position updates and the user is outside of the geoFencePolygon. Takes an optional object containing options for accuracy, timeout and age.

** Things to note about order of lat long points in polygon array. The order of the points are very important. They should be entered in the order you would you would draw them. Think about it like a connect the dots drawing, you need to start with a specific point and end with a specific point if you want the polygon to be correct.
*** Even though the example only has 4 points you can have as many as you would like.

```javascript
var fence;
var polygon = [
{lat: 34.045303, lon: -118.334650}, // top left
{lat: 34.045252, lon: -118.334462}, // top right
{lat: 34.045131, lon: -118.334498}, // bottom right
{lat: 34.045185, lon: -118.334684}, // bottom left
];
function setup(){

//optional options object for geoFencegeoFencePolygon
//fence = new geoFenceCircle(polygon, insideTheFence, 'mi', fenceOptions)
// fenceOptions = {
// enableHighAccuracy: false,
// timeout: 5000,
// maximumAge: 0
// };

fence = new geoFenceCircle(polygon, insideTheFence, outsideTheFence, 'mi')
}

function insideTheFence(position){
print("INlat: " + position.latitude);
print("INlong: " + position.longitude);
print("user is inside of the fence")
}

function outsideTheFence(position){
print("OUTlat: " + position.latitude);
print("OUTlong: " + position.longitude);
print("user is outside of the fence")
}
```
#### geoFencePolygon() insideFence boolean
###### geoFencePolygon(ArrayOfObjectsWithLatLong, insideCallback, outsideCallback, units, options)
geoFencePolygon also has a useful parameter for checking the fence status. .insideFence when called on your geoFencePolygon object will return true or false depending on the users relationship to the fence.
```javascript
var fence;
{lat: 34.045303, lon: -118.334650}, // top left
{lat: 34.045252, lon: -118.334462}, // top right
{lat: 34.045131, lon: -118.334498}, // bottom right
{lat: 34.045185, lon: -118.334684}, // bottom left
];
function setup(){
fence = new geoFencePolygon(polygon)
}

function draw(){
print(fence.insideFence);
}
```

0 comments on commit cec29ee

Please sign in to comment.