Where, that uses map and known locations

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.

All you should need to do to make this work, is enter who in the first var with the XXX.

Next, add their number in the second var. there is three option on formatting that wireless carriers use. +1XXXXXXXXXX
 1XXXXXXXXXX
   XXXXXXXXXX

Final is what you want them say that will allow your phone to send the message. example("where", "where are you", "where?")

Any question, find me in the OnX forums. Search for the Where? topic.

Always happy to help with what i know!!!!

Most of this is provided by Davin!! Gotta give credit!!

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         //
////////////////////////////

Recipe Wall

841

Views