{"id":1387,"date":"2016-09-13T12:58:52","date_gmt":"2016-09-13T19:58:52","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=1387"},"modified":"2016-09-13T12:58:52","modified_gmt":"2016-09-13T19:58:52","slug":"using-firebase-in-a-real-app-here-i-am-gps-demo-app","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2016\/09\/13\/using-firebase-in-a-real-app-here-i-am-gps-demo-app\/","title":{"rendered":"Using Firebase in a real app &#8211; &#8220;Here I AM!&#8221; GPS Demo app"},"content":{"rendered":"<p>A <a href=\"http:\/\/appinventor.pevest.com\/?p=1369\">previous post<\/a> introduced the experimental Firebase component in MIT App Inventor. Firebase is a cloud-based database that may be used by App Inventor apps, and which makes exchanging data between users easy. <em>Note: Firebase is experimental and may change in the future. At the present time, there is one Firebase database that is shared by all users. See comments in <a href=\"http:\/\/appinventor.pevest.com\/?p=1369\">previous post<\/a> for more information<\/em>.<\/p>\n<p>This post\u00a0uses the GPS location sensors in phones to determine the phone&#8217;s position in latitude and longitude. Pressing a button labeled &#8220;Send GPS&#8221; sends the latitude and longitude coordinates to another phone, which then draws the position on a map. In this way, two phone users can wander about and transmit their location to each other, sending the data through the Firebase cloud database. I named this app &#8220;Here I AM!&#8221; &#8211; pressing the Send GPS tells another user that &#8220;I am here!&#8221;<\/p>\n<h2>User Interface<\/h2>\n<p>To use this app, the exact same app should be running on two separate phones. You can run it on a single phone but obviously, you will not be getting updates from anyone else!<\/p>\n<p><em>The first time you run this app on Android 6, you will be asked to give permission to use location information. Also, be sure to set Location services to on, in Android Settings.<\/em><\/p>\n<p>As you move about, press the <em>Send GPS<\/em> button. \u00a0Your latitude and longitude will be sent to Firebase, the other app will be alerted to an updated location, and your location will be displayed on the Google maps on their phone. If they press<em> Send GPS<\/em>, then their location will be updated on <em>your<\/em> phone.<\/p>\n<p>The\u00a0Lat\/Long. textbox displays\u00a0either the latitude, longitude or is\u00a0prefixed with &#8220;THEM:&#8221;. &#8220;THEM:&#8221; means the textbox is showing the other phone&#8217;s location; if &#8220;THEM:&#8221; is not shown, then the textbox is showing your own latitude, longitude.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Screenshot_20160913-102744.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1389\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Screenshot_20160913-102744-576x1024.gif\" alt=\"Screenshot_20160913-102744\" width=\"234\" height=\"416\" \/><\/a><\/p>\n<p>Press <em>Retrieve GPS<\/em> to retrieve and display the latitude\/longitude value currently stored in Firebase.<\/p>\n<p>Press <em>Show Map<\/em> to display the \u00a0last retrieved\u00a0location on the map.<\/p>\n<p><!--more--><\/p>\n<p>Latitude and longitude values partially obscured, intentionally, in the above screen shot.<\/p>\n<h2>Designer View<\/h2>\n<p>User a horizontal layout to arrange 3 buttons across the top, and then a 2nd horizontal arrangement for the Lat\/Long label and the textbox (you could also use a label instead of the textbox as this is not really an input field).<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/DesignerUI.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1390\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/DesignerUI.gif\" alt=\"DesignerUI\" width=\"263\" height=\"433\" \/><\/a><\/p>\n<p>Note the use of the FirebaseDB1 and LocationSensor1 components; both are non-visible and appear below the user interface screen view in the Designer View.<\/p>\n<p>The Components list shows a Vertical Arrangement but this is not needed; an earlier version of the app had stacked the buttons and text vertically.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Components.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1394\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Components.gif\" alt=\"Components\" width=\"167\" height=\"248\" \/><\/a><\/p>\n<p>FirebaseDB is located in the Experimental section of the Palette.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Firebase.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1391\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/Firebase.gif\" alt=\"Firebase\" width=\"202\" height=\"351\" \/><\/a><\/p>\n<p>The LocationSensor accesses the location information (such as GPS) and is located in the Sensors section of the Palette.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/LocationSensor.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1396\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/LocationSensor.gif\" alt=\"LocationSensor\" width=\"184\" height=\"443\" \/><\/a><\/p>\n<h2>Blocks View<\/h2>\n<p>The app defines a &#8220;tag&#8221; value used in storing and retrieving a data record from Firebase. (For more on databases and &#8220;tags&#8221; and &#8220;records&#8221;, please see <a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">App Inventor 2 Databases and Files e-book, available from Amazon, Google and Kobo books<\/a>. Note that Firebase is not a supported feature &#8211; the ebook covers TinyDB, TinyWebDB, Fusion Tables and file access &#8211; but not yet Firebase.)<\/p>\n<p>We define <em>OurLocation<\/em> as a text string to store our latitude and longitude.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/SendGPSBtn.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1392\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/SendGPSBtn.gif\" alt=\"SendGPSBtn\" width=\"481\" height=\"309\" \/><\/a><\/p>\n<p>When the <em>Send GPS<\/em> button is pressed, we first check to see if the <em>LocationSensor<\/em> has valid location data by checking the <em>HasLongitudeLatitude<\/em> property. If the location is up to date, then the <em>Latitude<\/em> and <em>Longitude<\/em> properties are used to set <em>OurLocation<\/em> and this value is stored in the FirebaseDB.<\/p>\n<p>The<em> Retrieve GPS<\/em> button initiates a read of the value corresponding to the <em>GPSTagValue<\/em>. Note that FirebaseDB data reads are asynchronous. This means we start the process but the value will not be immediately available (due to network delays, other database users, server speed and so on). Instead, when the data is available, the <em>GotValue<\/em> event occurs and the value is then read.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/RetrieveGPS.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1397\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/RetrieveGPS.gif\" alt=\"RetrieveGPS\" width=\"366\" height=\"102\" \/><\/a><\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/GotValue.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1398\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/GotValue.gif\" alt=\"GotValue\" width=\"297\" height=\"100\" \/><\/a><\/p>\n<p>FirebaseDB has a feature that makes it easy to be alerted to updates in the database, such as when the other user of this app pressed <em>Send GPS<\/em> and updates the location in the database. Our app receives a <em>DataChanged<\/em> event. In this example, <em>DataChange<\/em> tells us which record was changed by giving us the &#8220;tag&#8221; value, plus the <em>value<\/em> of the record. As our example has only a single tag, we ignore the returned tag value. If\u00a0the <em>value<\/em> in the database is identical to our own location, then our app was just alerted to us changing our own location! We can ignore being alerted to our own update!<\/p>\n<p>If the location value is different than our current location, we assume it is an update from the other user and process that update but displaying their latitude and longitude and dropping a pin on a Google map.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/DataChanged.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1399\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/DataChanged.gif\" alt=\"DataChanged\" width=\"468\" height=\"208\" \/><\/a><\/p>\n<p>The <em>Show Map<\/em> button calls a procedure <em>ShowPositionOnMap<\/em> with our current location.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/ShowMap.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1400\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/ShowMap.gif\" alt=\"ShowMap\" width=\"349\" height=\"109\" \/><\/a><\/p>\n<p>The <em>ShowPositionOnMap<\/em> procedure uses Google Maps to do the hard work of drawing the map and pin location. We use the <em>WebViewer<\/em> control to access the Google Maps URL. By forming the URL in a special way, Google Maps is asked\u00a0to drop a pin on the map at the specified latitude and longitude.<\/p>\n<p>We build up a string that is in the format:<\/p>\n<p>https:\/\/www.google.com\/maps?&#038;z=10&#038;q=45.33088+-122.00877&#038;ll=45.33088+-122.00877<\/p>\n<p>The &amp;z, &amp;q and &amp;ll are &#8220;parameter&#8221; values passed to Google Maps, and which\u00a0instruct Maps to draw the map near the specified lat\/long and to drop a pin at the specific lat\/long.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/ShowPositionOnMap.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1401\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/ShowPositionOnMap.gif\" alt=\"ShowPositionOnMap\" width=\"511\" height=\"148\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>This app demonstrates the use of FirebaseDB in a sample real world application. This app could be improved to work with multiple phones, by having each instance of the app configure a unique user name. Then, the user name could be passed in a tag value for each update and the code could be modified to display where each user is currently positioned.<\/p>\n<p>The app could also be made to send automatic updates (rather than pressing <em>Send GPS<\/em>). This can be done by using the <em>LocationChanged<\/em> event of the <em>LocationSensor<\/em>:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/LocationChanged.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1402\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2016\/09\/LocationChanged.gif\" alt=\"LocationChanged\" width=\"262\" height=\"80\" \/><\/a>However, the GPS sensor and constant updates will drain the battery quite quickly.<\/p>\n<p>This app could also be implemented using TinyWebDB, instead of FirebaseDB, but FirebaseDB&#8217;s automatic update alert makes this easier.<\/p>\n<p>I tested this app on a Nexus 4 and a Nexus 5. The purpose of this app is to demonstrate <em>FirebaseDB<\/em> and the the GPS <em>LocationSensor<\/em>. It has not been written or tested for use as a &#8220;production&#8221; application; it is intended for educational purposes only.<\/p>\n<h2>Download<\/h2>\n<p>Source code: <a href=\"http:\/\/appinventor.pevest.com\/source\/tutorials\/Here_I_AM.aia\">Here_I_AM.aia<\/a><\/p>\n<p>App Inventor Gallery: <a href=\"http:\/\/ai2.appinventor.mit.edu\/?galleryId=5665698650259456\">Firebase GPS Demo<\/a>\u00a0(Use this link to load the app directly into App Inventor from the MIT App Inventor Gallery)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A previous post introduced the experimental Firebase component in MIT App Inventor. Firebase is a cloud-based database that may be used by App Inventor apps, and which makes exchanging data between users easy. Note: Firebase is experimental and may change in the future. At the present time, there is one Firebase database that is shared &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2016\/09\/13\/using-firebase-in-a-real-app-here-i-am-gps-demo-app\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Firebase in a real app &#8211; &#8220;Here I AM!&#8221; GPS Demo app<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1389,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1387","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=1387"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/1387\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media\/1389"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=1387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=1387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=1387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}