Created
September 9, 2012 20:49
-
-
Save error454/3687194 to your computer and use it in GitHub Desktop.
on{X} Send Location via Google Maps
This file contains 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 numbers = new Array("5035551234", "5035551235", ""); | |
var messageText = "where?"; | |
console.log('Started script: Send location to number using google maps'); | |
// Register callback on sms received event | |
device.messaging.on('smsReceived', function (sms) { | |
//Allow using +1 or not +1 in phone number definition | |
var sanitizedFrom = sms.data.from; | |
if(sanitizedFrom.split("+1").length == 2){ | |
sanitizedFrom = sanitizedFrom.split("+1")[1]; | |
} | |
if ( (numbers.indexOf(sanitizedFrom) != -1 || numbers.indexOf(sms.data.from) != -1) | |
&& sms.data.body.toLowerCase() === messageText.toLowerCase()) { | |
// getting location from cell, which is accurate enough in this case, time interval is 100 milliseconds, to get immediate location sample | |
var locListener = device.location.createListener('CELL', 100); | |
locListener.on('changed', function (signal) { | |
// stop listening to location changed events after getting the current location | |
locListener.stop(); | |
var mapURL = 'http://maps.google.com/maps?z=12&t=m&q=loc:'; | |
mapURL += signal.location.latitude + "+" + signal.location.longitude; | |
// sending text message with the current location | |
device.messaging.sendSms({ | |
to: sms.data.from, | |
body: 'Hi, I am here: ' + mapURL | |
}, | |
function (err) { | |
if (err) { | |
console.error('Error sending text message: ' + JSON.stringify(err)); | |
} | |
} | |
); | |
}); | |
locListener.start(); | |
} | |
}); | |
console.log('Completed script: Send location to number using google maps'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment