Skip to content

Instantly share code, notes, and snippets.

@dristic
Forked from ToeJamson/1.js
Last active January 20, 2022 18:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dristic/6225305 to your computer and use it in GitHub Desktop.
Save dristic/6225305 to your computer and use it in GitHub Desktop.
navigator.geolocation.getCurrentPosition(function (position) {
console.log(“I am located at: “ + position.coords.latitude + “, “ + position.coords.longitude);
});
navigator.geolocation.watchPosition(function (position) {
console.log(“I am now located at: “ + position.coords.latitude + “, “ + position.coords.longitude);
});
calculateDistance = function(lat1, lon1, lat2, lon2) {
var R, a, c, d, dLat, dLon;
R = 6371;
dLat = (lat2 - lat1).toRad();
dLon = (lon2 - lon1).toRad();
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
d = R * c;
return d;
};
Number.prototype.toRad = function() {
return this * Math.PI / 180;
};
///////////////////////////////////////////////////
// Client code (JavaScript)
///////////////////////////////////////////////////
var uuid = PUBNUB.uuid();
// PubNub and application initialization
pubnub.subscribe({
channel: uuid,
callback: function (data) {
}
});
// Do something with the nodes
navigator.geolocation.getCurrentPosition(function (position) {
var currentPos = position;
pubnub.publish({
channel: 'getNodes',
message: {
name: 'AUser',
uuid: uuid,
location: {
latitude: currentPos.coords.latitude,
longitude: currentPos.coords.longitude
}
}
});
});
///////////////////////////////////////////////////
// Server code (JavaScript, NodeJS)
///////////////////////////////////////////////////
pubnub.subscribe({
channel: 'getNodes',
callback: function (data) {
// Find the nodes the user is in using data.lat and data.long
var insideNodes = findInsideNodes(data.lat, data.long);
pubnub.publish({
channel: data.uuid,
message: {
type: 'getNodes',
inside: insideNodes
}
});
}
});
@epsa-dev
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment