<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suhaib Khan on Medium]]></title>
        <description><![CDATA[Stories by Suhaib Khan on Medium]]></description>
        <link>https://medium.com/@saibby?source=rss-5518fcc172dc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BMGC2a-0ssaUBJDkzToYfw.jpeg</url>
            <title>Stories by Suhaib Khan on Medium</title>
            <link>https://medium.com/@saibby?source=rss-5518fcc172dc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 17:51:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saibby/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building a Real-time Location Tracking app with NativeScript-Vue under 350 lines of code]]></title>
            <link>https://blog.saibbyweb.com/building-a-real-time-location-tracking-app-with-nativescript-vue-under-350-lines-of-code-8b51ad40d657?source=rss-5518fcc172dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b51ad40d657</guid>
            <category><![CDATA[app-development]]></category>
            <category><![CDATA[nativescript-vue]]></category>
            <category><![CDATA[nativescript]]></category>
            <category><![CDATA[location-tracking-app]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Suhaib Khan]]></dc:creator>
            <pubDate>Thu, 14 Feb 2019 17:18:45 GMT</pubDate>
            <atom:updated>2023-03-27T00:55:04.090Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UyOsdQuYRlLSkPCSVTqmKg.jpeg" /></figure><h3>Building a Real-time Location Tracking app with NativeScript-Vue under 350 lines of code 🔥</h3><p>I recently tried my hands on NativeScript-Vue, just out of curiosity to experience how the most starred* JavaScript Framework on GitHub can help me in my app development process. Just 45 minutes into it, I ended up having this Real-time Location tracking app working flawlessly on both iOS and Android. In this write-up, I’m gonna walk you through the process of building this app from scratch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*-FD0eT1H-NgUdOzwo3to7w.gif" /></figure><p>Assuming :</p><ul><li>Your system is setup to compile apps as communicated in this <a href="https://nativescript-vue.org/en/docs/getting-started/installation/">Nativescript-Vue Installation Manual</a></li><li>You have basic knowledge about <a href="https://vuejs.org/v2/guide/">Vue.js</a> [data, created, methods,mixins] and NativeScript</li></ul><p>Let’s just jump into it.</p><blockquote>Non MacOS users like me need <a href="https://www.nativescript.org/nativescript-sidekick">Nativescript Sidekick</a> installed to be able to run iOS apps.</blockquote><h3>Step 1: Project Setup</h3><p>First, lets create a new project based on the <a href="https://github.com/nativescript-vue/vue-cli-template">vue-cli-template</a> (a blank Nativescript-Vue app) using terminal.</p><pre>npm install -g @vue/cli @vue/cli-init<br>vue init nativescript-vue/vue-cli-template &lt;project-name&gt;<br>cd &lt;project-name&gt;<br>npm install</pre><ol><li>Installs the latest Vue CLI and support for Vue CLI 2.x templates through the @vue/cli-init add-on.</li><li>Creates a project using the <a href="https://github.com/nativescript-vue/vue-cli-template">vue-cli-template</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*U2I8tB8_8YaMMdE9toxxew.png" /><figcaption>Project configuration</figcaption></figure><p>3. Switches to the directory containing the newly created project.</p><p>4. Installs npm dependencies locally.</p><h3>Step 2: Setting up project dependencies</h3><p>For this project, we are going to use three NativeScript Plugins. Install them using NativeScript CLI tooling:</p><ul><li><a href="https://market.nativescript.org/plugins/nativescript-google-maps-sdk"><strong>NativeScript Google Maps SDK</strong></a><br>For rendering Google Maps, routes, markers etc. on the app.</li></ul><pre>tns plugin add nativescript-google-maps-sdk</pre><ul><li><a href="https://market.nativescript.org/plugins/nativescript-geolocation"><strong>NativeScript Geolocation</strong></a><br>For fetching device location.</li></ul><pre>tns plugin add nativescript-geolocation</pre><ul><li><a href="https://market.nativescript.org/plugins/nativescript-permissions"><strong>NativeScript Permissions</strong></a><br>To ask and gain for location access from user.</li></ul><pre>tns plugin add nativescript-permissions</pre><p>Also, one npm package <a href="https://www.npmjs.com/package/decode-google-map-polyline">decode-google-map-polyline</a> will be required for drawing routes on the map. Install it using:</p><pre>npm install decode-google-map-polyline</pre><h3>Step 3: Getting the API Key(s)</h3><p>Only registered Google users can gain access to API key(s) required for Maps to work properly. Key(s) can be obtained from <a href="https://console.cloud.google.com/google/maps-apis/overview">Google Cloud Console</a>.</p><p>For our project we need access to 4 Google APIs:</p><ol><li><a href="https://developers.google.com/maps/documentation/android-sdk/intro"><strong>Maps SDK for Android</strong></a><br>For rendering maps on Android devices.</li><li><a href="https://developers.google.com/maps/documentation/ios-sdk/get-api-key"><strong>Maps SDK for iOS</strong></a><br>For rendering maps on iOS devices.</li><li><a href="https://developers.google.com/maps/documentation/directions/start"><strong>Directions API</strong></a><br>For getting routes between locations.</li><li><a href="https://developers.google.com/maps/documentation/distance-matrix/get-api-key"><strong>Distance Matrix API</strong></a><br>For calculating distance and Estimated Time of Arrival.</li></ol><p>Clicking on the above 4 mentioned APIs will redirect you to their official documentation and also guide you on getting hold of the API key(s).</p><blockquote>Google Cloud Console will ask you to <strong>create a project</strong> before starting to mess up with the APIs. Once you have created a project and <strong>a key is issued</strong> for an API, subsequent enabled APIs will be accessible using the <strong>same key</strong> which was issued for the<strong> first API</strong>. That means, the 4 APIs listed above can have <strong>one single key</strong> if falling under the same project, which exactly is the case with this app.</blockquote><h3>Step 4: Setting up the API key</h3><p>Now that we have got our precious <strong>key</strong> with all required APIs enabled, we can proceed by letting Android and iOS know about it.</p><p><strong>For Android</strong> : Modify<strong> </strong>AndroidManifest.xml<strong> </strong>located at app/App_Resources/Android/src/main/AndroidManifest.xml and insert this between &lt;application&gt; tags.</p><pre>&lt;meta-data android:name=&quot;com.google.android.geo.API_KEY&quot; android:value=&quot;PUT_API_KEY_HERE&quot; /&gt;</pre><p><strong>For iOS</strong>: Add the following snippet at the top of main.js</p><pre>import * as platform from &#39;platform&#39;</pre><pre>if (platform.isIOS) { <br>  GMSServices.provideAPIKey(&quot;PUT_API_KEY_HERE&quot;)<br>}</pre><p>Replace PUT_API_KEY_HERE with the actual key.</p><h3>Step 5: Registering MapView Element with Vue</h3><p>The Google Maps plugin we are using, does not support Vue out-of-the-box, we need to manually register MapView element in JS to be able to use it in our template layout. Add the following line of code to main.js :</p><pre>Vue.registerElement(&#39;MapView&#39;, ()=&gt; require(&#39;nativescript-google-maps-sdk&#39;).MapView)</pre><p>After this step, our main.js should look something like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc671536df3c7d131fcf739c0342828b/href">https://medium.com/media/bc671536df3c7d131fcf739c0342828b/href</a></iframe><h3>Step 6 : The Layout</h3><p>As this project was initialized from vue-cli-template, you might have noticed main.js is importing a component named App located at ./components/App.vue. The actual layout and business logic of the app is defined inside this component, so lets start re-writing App.vue to our own specifications. At this point, we can remove everything present inside the file and paste the following template :</p><blockquote>Keep in mind, we are going to use some method names and properties in this template for event handlers and data binding, which as of now are undefined and will be defined later in the <em>&lt;script&gt;</em> part of this component.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84769fbc311788bfcd2a27b273085715/href">https://medium.com/media/84769fbc311788bfcd2a27b273085715/href</a></iframe><p>Visually, we have divided our UI into three main sections:</p><ul><li>The top most part is the control center, consists of 4 buttons, each having its own @tap event handler :<br>1. Get Directions<br>2. Clear Route<br>3. Start Journey<br>4. End Journey</li><li>The middle part is the actual Google MapView having @mapReady (when the map has completed rendering) and @coordinateLongPressed (when a point on the map is long tapped) event handlers in place. Also, latitude,longitude and zoom attributes of the element are binded to the origin and zoom properties of the component respectively. Plus, the MapView only renders if the allowExecution property is set to true.</li><li>Lastly, the bottom part is simply a TextView binded to journeyDetails property to print the status of our journey.</li></ul><h4>Normalizing the layout :</h4><p>A little bit of styling wont hurt, so let’s add the &lt;style&gt; section of our Vue component to bring some colors and alignment in place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f196efe2ea54b9e71fe2a9fc688dc082/href">https://medium.com/media/f196efe2ea54b9e71fe2a9fc688dc082/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CSW_-FxpEb3TDBP9dpS8eg.jpeg" /><figcaption>Before/After Styling</figcaption></figure><h3>Step 7: Business Logic</h3><p>Now that layout is done, we will start writing the &lt;script&gt; part of the component containing the business logic. But before starting let’s see an overview of what the script is gonna look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38574be851aee6fe7af8520048da1774/href">https://medium.com/media/38574be851aee6fe7af8520048da1774/href</a></iframe><h4>7.1 Data function:</h4><p>returning data specific to the template part and permissions i,e:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cb8c0af59a941532ce6b9352b12d7ab/href">https://medium.com/media/0cb8c0af59a941532ce6b9352b12d7ab/href</a></iframe><p><strong>Quick Summary:</strong></p><p>origin : having co-ordinates fed to the map.<br>destination : storing destination co-ordinates.<br>journeyDetails : binded to the &lt;TextView&gt; to display journey details.<br>allowExecution : will hold location permission access status for Android.<br>journeyStarted : will hold journey status.<br>mapView : will be used to store the rendered map instance. <br>zoom : adjusting camera zoom level.<br>APIKEY : reference to our enabled API key.</p><h4>7.2 Created Hook:</h4><p>Executed at the beginning, it will be used to request Location access on Android devices. iOS will automatically asks for permission when the app attempts to access it but for Android we have to manually pop up the permissions dialog to get it through and we are going to achieve it by, adding the permissions and platform import at the top of &lt;script&gt; tags:</p><pre>import * as permissions from &#39;nativescript-permissions&#39;<br>import * as platform from &#39;platform&#39;</pre><p>and then write the permission requesting routine:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b79a7136d31172869029b5fe49a2ff32/href">https://medium.com/media/b79a7136d31172869029b5fe49a2ff32/href</a></iframe><p>Also, add the following permissions to app/App_Resources/Android/src/main/AndroidManifest.xml :</p><pre>&lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;    <br>&lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;</pre><h4>7.3 Methods Object:</h4><p>This is where all the magic happens. As stated above, it will consist of 6 methods used as event handlers in our template. Instead of writing the actual code, I have commented what exactly these methods are going to do. Go through this once, to get an idea.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc995f39f429d6463c55201eb3f4bdbb/href">https://medium.com/media/dc995f39f429d6463c55201eb3f4bdbb/href</a></iframe><p>For most of the commented lines above, we’ll be calling methods defined in <strong>another file, </strong>that will merge with our component options using Mixins . We could have defined everything in App.vue only, but sharing some space with helper Mixins will make our code more maintainable. We’ll come back to this methods object after we finish writing our helper objects.</p><h4>7.4 Mixins</h4><p>Create a new file named MapsHelper.js in/app/components/ and add the following imports at the top of the file :</p><pre>import * as http from &quot;http&quot;;<br>import * as geolocation from &quot;nativescript-geolocation&quot;;<br>import { Accuracy } from &quot;ui/enums&quot;;<br>import * as platform from &quot;platform&quot;;<br>import * as decodePolyline from &quot;decode-google-map-polyline&quot;;<br>import { Position, Marker, Polyline, Bounds } from &quot;nativescript-google-maps-sdk&quot;;</pre><p><strong>Quick Summary:</strong></p><p>http: for making http(s) requests.<br>geolocation : to access device location.<br>Accuracy : used by geolocation for imporving location accuracy.<br>platform : to detect device type. <br>decodePolyline : for decoding encoded points provided by Directions API. Position : creates a Position object from coordinates (fed to the map).<br>Marker : for adding marker(s) to the map. <br>Polyline : for drawing route(s) on the map. <br>Bounds : for setting camera view bounds (portion of map visible).</p><p>Rest of this file is going to 4 objects, namely, <br>a) MapsUIHelper<br>b) DirectionAPIHelper<br>c) DistanceMatrixAPIHelper<br>d) LocationHelper</p><p>that we’ll <strong>ship</strong> collectively at the end of the file and then import in App.vue to use as mixins. Most of the code below is self explanatory, so I won’t go into details, however objectives of each object are listed.</p><p><strong>7.4.1 MapsUIHelper</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/393dad6e2ecf7fb703c58fb62fa08d14/href">https://medium.com/media/393dad6e2ecf7fb703c58fb62fa08d14/href</a></iframe><p><strong>7.4.2 DirectionsAPIHelper</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f00e4b47df4847e7b1feab96f71b9fb2/href">https://medium.com/media/f00e4b47df4847e7b1feab96f71b9fb2/href</a></iframe><p><strong>7.4.3 DistanceMatrixAPIHelper</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a76fff0a0d955b48d6c0fa8e1ed4ca7/href">https://medium.com/media/9a76fff0a0d955b48d6c0fa8e1ed4ca7/href</a></iframe><p><strong>7.4.4 LocationHelper</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64271a7b7501e33c1f0a1f1f1fc1887a/href">https://medium.com/media/64271a7b7501e33c1f0a1f1f1fc1887a/href</a></iframe><p>The final part of the file would be to export all these components:</p><pre>export default { MapsUIHelper, DirectionsAPIHelper, DistanceMatrixAPIHelper, LocationHelper };</pre><p>Coming back to App.vue, lets import the helpers,</p><pre>import MapsHelper from &quot;./MapsHelper.js&quot;;</pre><p>update Mixins array,</p><pre>mixins: [ MapsHelper.MapsUIHelper, MapsHelper.DirectionsAPIHelper, MapsHelper.DistanceMatrixAPIHelper, MapsHelper.LocationHelper ]</pre><p>and (re) write methods :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bb00341abcfc43217530dcd6c1509ee/href">https://medium.com/media/1bb00341abcfc43217530dcd6c1509ee/href</a></iframe><blockquote>The nativescript-google-maps-sdk plugin we are using (at the time of writing) has some wierd iOS bug causing the map center to move up to the top-left corner. It can be fixed by uncommenting the 6 lines below/* ios map center bug fix */ block in mapReady method. More info at this <a href="https://stackoverflow.com/questions/54218519/wrong-map-center-in-ios-using-nativescript-google-maps-sdk/54445615#54445615">SO answer</a>.</blockquote><h4>That’s it! Our app is ready.</h4><p>Try tns run android / tns run ios to test. For iOS provisioning profile and certificate, I used NativeScript Sidekick to automate the process. If something didn’t work for you, feel free to comment down the issue and I will respond.</p><h3>Show me the code !?</h3><p>Here, you go : <a href="https://github.com/saibbyweb/Realtime-Location-Tracking-NativeScript-Vue">GitHub : saibbyweb/RLTNSVue</a></p><p>This is my first ever Medium write up, if it was helpful in anyway, do show your support by following my publication <a href="https://medium.com/saibbyweb">@saibbyweb</a> or visit my website for more <a href="https://saibbyweb.com/">web and app development</a> related stuff.</p><p>Read More: <a href="https://hyperhits.net/lifestyle/adidas-somas-a-non-existent-cousin-of-samoa">Adidas Somas</a></p><p>EDIT : Thanks <a href="https://medium.com/u/d7e6a1fa195b">丁丁(DingDing)</a> for pointing out the mistakes :)</p><p>Written with ❤ in Kashmir.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b51ad40d657" width="1" height="1" alt=""><hr><p><a href="https://blog.saibbyweb.com/building-a-real-time-location-tracking-app-with-nativescript-vue-under-350-lines-of-code-8b51ad40d657">Building a Real-time Location Tracking app with NativeScript-Vue under 350 lines of code</a> was originally published in <a href="https://blog.saibbyweb.com/">saibbyweb</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>