-
-
Save dristic/6225305 to your computer and use it in GitHub Desktop.
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
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); | |
}); |
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
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; | |
}; |
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
/////////////////////////////////////////////////// | |
// 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 | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No esta completo el ejemplo en el blog de PubNub:
https://www.pubnub.com/blog/2013-08-13-building-real-time-geolocation-apps-with-javascript-and-pubnub/