When someone whom you declare sends you are message your phone can reply automatically your location. This has been updated to use goefences for known locations. So i they text where and your home then it'll say at home.
To use this on{X} Recipe, simply copy the code below and modify if needed.
// You can use http://www.getlatlon.com to get the lat/lon of a location.
////////////////////////////
// Begin Setup Variables //
////////////////////////////
var locations = [
////////////////////////////
// Add fences here //
////////////////////////////
{name:'My House', latitude:'XXXXXXXX', longitude:'XXXXXXXX', radius:100},
{name:'OReillys', latitude:'XXXXXXX ', longitude:'XXXXXXX', radius:100},
{name:'Deaville Home', latitude:'XXXXX', longitude:'XXXXXXX', radius:100},
{name:'Shop', latitude:'XXXXX', longitude:'XXXXXX', radius:50}
////////////////////////////
// Stop adding fences //
////////////////////////////
];
var friend = {name:'Kimber', phoneNumber:'805XXXXX'};
// {name:'Gabe', phoneNumber:'805XXXXX'};
//Friend to respond to
var messageText = 'where are you'; // The exact text to respnd to
var provider = 'CELL'; // Location Provider type to use (GPS,CELL,PASSIVE,HYBRID)
////////////////////////////
// End Setup Variables //
////////////////////////////
////////////////////////////
// Begin Rule Logic //
////////////////////////////
//This is to avoid double text message issue I was getting
var triggered = false;
var physicalLoc = '';
// Convert value to radians
function toRad(value) {
return value * Math.PI / 180;
}
// Is a given point with a given geofence?
function inGeoFence(currLat, currLong, geoLat, geoLong, geoRadius) {
//Derived from http://www.movable-type.co.uk/scripts/latlong.html
var R, dLat, dLon, a, c, d;
R = 6371; // mean radius of Earth in km
dLat = toRad((geoLat - currLat));
dLon = toRad((geoLong - currLong));
currLat = toRad(currLat);
geoLat = toRad(geoLat);
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(currLat) * Math.cos(geoLat);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
d = R * c * 1000;
return d > geoRadius ? false : true;
}
console.log('////////////////////////////');
console.log('// Starting up the script //');
console.log('////////////////////////////');
// Register callback on sms received event
device.messaging.on('smsReceived', function (sms) {
if (sms.data.from === friend.phoneNumber && sms.data.body.toLowerCase() === messageText) {
//We got a "where?" text message
console.log('Recieved a "where?" text.');
//Reset triggered value
triggered = false;
// getting location from the provider specified above, time interval is 100 milliseconds
var locListener = device.location.createListener(provider, 100);
locListener.on('changed', function (signal) {
// stop listening to location changed events after getting the current location
locListener.stop();
if(!triggered) {
triggered = true;
var mapUrlPattern = 'http://maps.google.com/maps?z=1&t=m&q=loc:lat+lon';
var mapUrl = mapUrlPattern.replace(/lat/g, signal.location.latitude).replace(/lon/g, signal.location.longitude);
physicalLoc = '';
//Check current location against defined geo fences and we are in one change the mapUrl to the name of the location
for(x = 0; x < locations.length; x++) {
if(inGeoFence(signal.location.latitude, signal.location.longitude, locations[x].latitude, locations[x].longitude, locations[x].radius)) {
mapUrl = locations[x].name;
physicalLoc = locations[x].name;
break;
}
}
//Log current location
if(physicalLoc !== '') {
console.log('Current location according to the ' + provider + ' provider: ' + signal.location.latitude + ',' + signal.location.longitude + ' (' + physicalLoc + ')');
} else {
console.log('Current location according to the ' + provider + ' provider: ' + signal.location.latitude + ',' + signal.location.longitude);
}
// sending text message with the current location
device.messaging.sendSms({
to: friend.phoneNumber,
body: 'Hi, I am here: ' + mapUrl
}, function (err) {
if (err) {
console.error('Error sending text message: ' + JSON.stringify(err));
} else {
console.log('Successfully sent SMS.');
}
});
}
});
locListener.start();
}
});
////////////////////////////
// End Rule Logic //
////////////////////////////
Views