{"id":543,"date":"2017-04-25T15:46:23","date_gmt":"2017-04-25T15:46:23","guid":{"rendered":"http:\/\/bitcows.com\/?p=543"},"modified":"2017-04-25T15:46:23","modified_gmt":"2017-04-25T15:46:23","slug":"the-basics-of-appcelerator-geofencing","status":"publish","type":"post","link":"https:\/\/bitcows.com\/?p=543","title":{"rendered":"The Basics of Appcelerator Geofencing"},"content":{"rendered":"<p><strong>Caveat<\/strong><br \/>\nThis feature requires a Pro or Enterprise subscription!<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/platform.appcelerator.com\/#\/download\/modules\">Download the geofence module<\/a><\/li>\n<li>If you want to use Android simulator: <a href=\"https:\/\/www.genymotion.com\/\">Genymotion<\/a> 2.8.1+ installed for best results.<\/li>\n<li><a href=\"http:\/\/bitcows.com\/?p=586&#038;preview=true\">Install the correct files for google play<\/a> to use the Genymotion simulator<\/li>\n<\/ul>\n<p><strong>Setting up the tiapp.xml file<\/strong><br \/>\nFor iOS you will need to give your app some permissions alerting the user to the fact your using their location. <\/p>\n<pre class=\"prettyprint\">\n&lt;dict&gt;\n  &lt;key&gt;NSLocationAlwaysUsageDescription&lt;\/key&gt;\n  &lt;string&gt;We need your location so we can locate your current region location&lt;\/string&gt;\n  &lt;key&gt;NSLocationUsageDescription&lt;\/key&gt;\n  &lt;string&gt;We need your location so we can locate your current region location&lt;\/string&gt;\n&lt;\/dict&gt;\n<\/pre>\n<p><strong>Creating a geofence location<\/strong><br \/>\nThere are two ways to create a geofence location. One is through the Appcelerator dashboard and one is through code.<\/p>\n<p><strong>Using the Appcelerator Dashboard to create a geofence.<\/strong><br \/>\nLogin to the <a href=\"https:\/\/platform.appcelerator.com\">Appcelerator Desktop<\/a>. Under your arrow application, you&#8217;ll see Manage Data, then you should see the Geo Fences link in the table to the right. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bitcows.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-24-at-3.31.19-PM-1024x138.png\" alt=\"\" width=\"640\" height=\"86\"\/><\/p>\n<p>Once you are in that area you&#8217;ll want to create a new geofence. Fill out the few fields such as Geo Coordinates, Radius, and Payload and you should be set with your first geofence. <em>(NOTE: payload can be anything you want to parse in the app. I just needs to be in the form of a JSON object).<\/em> <\/p>\n<p>This creates a location that will be used in code to establish a center point and radius for your geofence to be created. It also provides any useful information about this point that you might want to display or keep track of.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bitcows.com\/wp-content\/uploads\/2017\/04\/Screen-Shot-2017-04-24-at-3.21.59-PM-1024x781.png\" alt=\"\" width=\"640\" height=\"488\"\/><\/p>\n<p><strong>Calling your geofences<\/strong><br \/>\nTo use our dashboard created geofences we must query for them using the Cloud <code>GeoFences.query<\/code><\/p>\n<pre class=\"prettyprint\">\n\/\/Add this at the top of the controller JS file\nvar Geofence = require(\"ti.geofence\")\nCloud = require('ti.cloud');\n\n\n\/\/ Notice longitude is first in the coordinates array.\n\/\/ Gets the geofences set up in arrow that are in a 2km area of your coordinates\nCloud.GeoFences.query({\n    where:{\n        \"loc\" : {\n                    \"$nearSphere\" : {\n                        \"$geometry\" : {\n                            \"type\" : \"Point\",\n                            \"coordinates\" : [-111.933395, 33.654489]\n                        },\n                        \"$maxDistance\" : 20000\n                    }\n                }\n}, function (e) {\n    if (e.success) {\n        Ti.API.info(JSON.stringify(e.geo_fences));   \n        \/\/ Loop our fences and create a region using the Geofence.createRegion\n        \/\/ Then add it to our startMonitoringForRegions array see code below\n        Geofence.createRegion({\n           center : {\n              latitude : e.geo_fences[i].loc.coordinates[1],\n              longitude : e.geo_fences[i].loc.coordinates[0],\n           },\n           radius : r[0],\n           identifier : e.geo_fences[i].payload.locationName + \"~\" + e.geo_fences[i].payload.placeId\n         })\n    } else {\n        alert('Error:\\n' +\n            ((e.error && e.message) || JSON.stringify(e)));\n    }\n});\n<\/pre>\n<p><strong>Creating geofence in code.<\/strong><br \/>\nTo create the region in code then you would do it like this. Setting up a center location point using GPS coordinates, establishing a radius, and giving it an identifier. <\/p>\n<pre class=\"prettyprint\">\nGeofence.createRegion({\n    center: {\n        latitude:33.654489,\n        longitude:-111.933395\n    },\n    radius:5, \/\/meters\n    identifier:'Axway Building'\n});\n<\/pre>\n<p><strong>Using the geofence<\/strong><br \/>\nThe main 3 events we want to focus on are start monitoring, enter region, and exit region. There are a few more but you can <a href=\"http:\/\/docs.appcelerator.com\/platform\/latest\/#!\/api\/Modules.Geofence-event-enterregions\">look those up<\/a> if you need them.<\/p>\n<pre class=\"prettyprint\">\nGeofence.startMonitoringForRegions([regions]);\nGeofence.addEventListener(\"enterregions\", function(e) {});\nGeofence.addEventListener(\"exitregions\", function(e) {});\n<\/pre>\n<p>The first thing you want to do after you have your geofence regions created is start monitoring them.<br \/>\nSo you would create your regions in code either dynamically using the cloud query, or statically. Then add each region to an array and send it to the Geofence.startMonitoringForRegions. This kicks off the monitor. <\/p>\n<p>After that you add event listeners to the Geofence object to listen for things like entering or exiting regions.<\/p>\n<p>Now if someone enters into any geofence area you have established you can send off and alert, or pop push a notification to a group of users, or what ever you can think of. <\/p>\n<p><a href=\"https:\/\/github.com\/djmason9\/Geofence-example\">Working code example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Caveat This feature requires a Pro or Enterprise subscription! Prerequisites Download the geofence module If you want to use Android simulator: Genymotion 2.8.1+ installed for best results. Install the correct files for google play to use the Genymotion simulator Setting up the tiapp.xml file For iOS you will need to give your app some permissions&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/bitcows.com\/?p=543\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":573,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5,15],"tags":[30,32,47,49],"class_list":["post-543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-appcelerator","category-ios","tag-android","tag-appc","tag-geofence","tag-ios-2"],"_links":{"self":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=543"}],"version-history":[{"count":0,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitcows.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}