Code Monkey home page Code Monkey logo

p5.geolocation's Introduction

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.

Useful Tips
License

p5.geolocaiton is licensed under the GNU LGPL 2.1.

p5.geolocation:

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 getCurrentPosition() 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 can be cleared using .clear()

var fence;
function setup(){
    fence = new geoFenceCircle(44.979779, -93.325499, 0.05, insideTheFence, outsideTheFence, 'mi')
}

function mousePressed(){
	fence.clear();
}

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 geoFencePolygon(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 can be cleared using .clear()

var fence;
function setup(){
    fence = new geoFenceCircle(44.979779, -93.325499, 0.05, insideTheFence, outsideTheFence, 'mi')
}

function mousePressed(){
	fence.clear();
}

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);
}

p5.geolocation's People

Contributors

bmoren avatar matteomenapace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

p5.geolocation's Issues

Seems to be broken with recent p5.js versions

Not sure, what exactly is failing, but I don't get this to work anymore with recent p5.js versions.
Was just about to test a script that I demonstrated in class last year, using p5.js v0.5.4 and it does not work anymore in v0.5.16. Not sure, at which version exactly it broke.

When I use the provided example for getCurrentPosition()

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)
}

it keeps loading forever (displaying the "Loading...", which indicates it is currently stuck in the preload function).

It does display the console output of p5.geolocation Loaded, so it loads the library for sure, but it is unable to get the current position.

ios 9 silently fails if location services turned off

When testing on ios 9 and if the users Settings -> Privacy -> Location services -> Safari are set to "Never" the webpage will not prompt for permission. There should maybe be a more explicit alert that notifies vs a console log. Will look into testing to see if we can do something more direct for ios / mobile.

set up github pages

we might need to use glitch also for any real-time or persistence issues.

Accuracy stuck at 65 on iPhone

When I use the watchPosition() my accuracy is always 65 but if I use the navigator.geolocation.watchPosition I get accuracy below 10. Anyone else having this problem?

new release?

Hello,

I want just to know if you plan to release a 0.6 version containing the latest commits.

Thank you!

geolocation denied (preload issue)

I put the getCurrentPosition() function in the preload function in my sketch. When the user denies geolocation in the browser it never leaves the preload function. The geocheck function will never fire since it won't reach the setup functionality. I also tried the error callback function but it still doesn't leave the preload function. Am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.