Skip to content

tstefanich/p5.geolocation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 

Repository files navigation

p5.geolocation

p5.geolocation

p5.geolocation provides techniques for acquiring, watching, calculating, and geofencing user locations for p5.js.

This activity is made possible by a research & planning grant from Forecast Public Art and the Jerome Foundation. Special thanks to Derek Anderson.

p5.geolocation:

Useful Tips
License

p5.geolocaiton is licensed under the GNU LGPL 2.1.

~ p5.geolocation examples ~

geoCheck()

geoCheck()

geoCheck() checks the availability of geolocation. Returns true if geolocation is available or false if geolocation is not available.

setup(){
	if(geoCheck() == true){
		//geolocation is available
	}else{
		//error getting geolocaion
	}
}

getCurrentPosition() used in preload()

getCurrentPosition(callback, errorCallback)

getCurrentPosition() can be used in preload() or with a callback (see below). When used in preload it will return an object containing position elements, latitude, longitude, altitude, etc..

var locationData;
function preload(){
    locationData =  getCurrentPosition();
}

function setup() {
    print(locationData.latitude)
    print(locationData.longitude)
    print(locationData.accuracy)
    print(locationData.altitude)
    print(locationData.altitudeAccuracy)
    print(locationData.heading)
    print(locationData.speed)
}

getCurrentPosition() used with a callback

getCurrentPosition(callback, errorCallback)

getCurrentPosition() can also be used with a callback. The callback fires once when the position data becomes available.

function setup(){
    getCurrentPosition(doThisOnLocation)
}

function doThisOnLocation(position){
    print("lat: "   position.latitude);
    print("long: "   position.longitude);
}

watchPosition() used with a callback

watchPosition(callback, errorCallback, options)

watchPosition() is very similar to getCurrentPosition(), except that it will fire it's callback each time the users position makes a noticable change. Takes an optional object containing options for accuracy, timeout and age.

//optional options for watchPosition()
//watchPosition(positionChanged, watchOptions)

// watchOptions = {
//   enableHighAccuracy: true,
//   timeout: 5000,
//   maximumAge: 0
// };

function setup(){
    watchPosition(positionChanged)
}

function positionChanged(position){
    print("lat: "   position.latitude);
    print("long: "   position.longitude);
}

clearWatch()

clearWatch()

clearWatch() cancels the watchPosition()

function mousePressed(){
	clearWatch();
	print("watchPosition() cleared")
}

intervalCurrentPosition() used with a callback

intervalCurrentPosition(callback, interval, errorCallback)

intervalCurrentPosition() is a hybrid of watchPosition() and getCurrentPosition(). It executes the getCurretnPosition() function on a interval in Milliseconds via an optional second parameter, default is 5000ms. This is useful when you need more nuanced changed location detection than watchPosition() can provide.

function setup(){
    intervalCurrentPosition(positionPing, 5000)
}

function positionPing(position){
    print("lat: "   position.latitude);
    print("long: "   position.longitude);
}

clearIntervalPos()

clearIntervalPos()

clearIntervalPos() cancels the intervalCurrentPosition()

function mousePressed(){
	clearIntervalPos();
	print("intervalCurrentPosition() cleared!")
}

calcGeoDistance()

calcGeoDistance(lat1, lon1, lat2, lon2, units)

calcGeoDistance() calculates the distance between two points in the provided units (default is 'mi', 'km' is also available).

var distance;
function setup(){
	distance = calcGeoDistance(46.785844, -92.015965, 44.940834, -93.311287, 'mi')
	print(distance);
}

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.

var fence;
function setup(){

	//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 geoFenceCircle(44.979779, -93.325499, 0.05, 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")
}

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.

var fence;
function setup(){
 	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.

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.

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(){
    fence = new geoFencePolygon(polygon)
}

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

About

a light weight geolocation library for p5.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%