-
-
Save jalbertbowden/c3a823e5281c0200c2ba to your computer and use it in GitHub Desktop.
Using KDTree for geospatial lookups (slightly experimental) (this is an untested example) via https://gist.github.com/sandfox/85f5ab526a9158b2cd30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!Doctype html> | |
| <html> | |
| <head> | |
| <script src="//cdnjs.cloudflare.com/ajax/libs/zepto/1.0rc1/zepto.min.js"></script> | |
| <script src="script.js"></script> | |
| </head> | |
| <a id="button" href="#"> | |
| <p>I AM A BUTTON</p> | |
| </a> | |
| <script type="text/javascript" src="kdtree.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var storeTree = []; | |
| var getNearest = function(location) { | |
| nearest = storeTree.getNearestNeighbour({ | |
| x: location.coords.longitude, | |
| y: location.coords.latitude | |
| }); | |
| //if you just more than one result then use this function instead - returns an array of objects | |
| /** | |
| * storeTree.getNearestNeighbours({ | |
| * x: location.coords.longitude, | |
| * y: location.coords.latitude | |
| *}, 1); //Number of results you want returned - they are always returned as an array | |
| */ | |
| //data/records/XXX/.json is arbitary path | |
| $.getJSON('data/records/' + nearest.id + '.json', | |
| function(returnedObject) { | |
| //Do something with the returned object | |
| }); | |
| }; | |
| var errorFunc = function(err){ | |
| console.log(err); | |
| } | |
| $(document).ready(function () { | |
| //Load up the index of stores - this is an odd way to do it | |
| $.getJSON('data/records/index.json', function(data){ | |
| if(Array.isArray(data)) { | |
| storeTree = new KDTree(data); | |
| } else { | |
| storeTree = new KDTree([]); | |
| storeTree.rootNode = data.rootNode; | |
| } | |
| }); | |
| $('#button').click(function () { | |
| navigator.geolocation.getCurrentPosition(getNearest, errorFunc, { enableHighAccuracy:true, timeout:1200, maximumAge:0 }); | |
| }) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment