Show ISS current position using ionic

January 8, 2016
Ionic is an HTML5 hybrid framework based on Cordova and Angular which allows you to build native mobile applications for Android, iOS and Windows Phone. In this tutorial, we will show you step-by-step how to use streamdata.io in an Ionic application to display the ISS position on a map in real-time.

If you just want to run the sample, after having installed Ionic and cloned the GitHub repository, you just need to:

Install Ionic and every dependencies needed


If you haven't already, follow this guide to install Ionic.

1 - Create the Ionic application


Start a new project based on the maps template by running ionic start iss.io maps. This will build an Ionic project which displays a map.

If you're working on Mac OS X, iOS platform has automatically been added to your project.

To add Android platform, run ionic platform add android.

You can run your application in your browser with ionic serve command, in an emulator with ionic emulate [platform] or in your device with ionic run [platform]. If you want to read more about those commands and their options, have a look here for serve and here for emulate and run.

When you run it for the first time, you should see a map centered on Ionic's headquarters, a header bar with title "Map" and a footer bar with a "center on me" button.

2 - Customize the application to match our needs


For our purpose, we need to display the map a little bit differently. Open the directives.js file in the www/js folder and follow steps below:

You can also replace the header bar title in index.html by something more relevant like ISS Position, and remove the code related to the footer bar and the "center on me" button for more readability if you prefer.

3 - Setting up the Streamdata.io event-source


In order to use the Streamdata.io proxy, you need an App Token. Register on the Portal to create an account and get a valid token. The Streamdata.io request for the ISS position looks like this:
https://streamdata.motwin.net/http://api.open-notify.org/iss-now.json?X-Sd-Token=[your-token]

As you can see, it is composed of three parts:

Let's call our request with a simple curl -XGET to see what the API send us:
event:data
data:{"iss_position":{"latitude":-5.154055625049744,"longitude":137.42699878015998},"message":"success","timestamp":1452186030}

event:patch
data:[{"op":"replace","path":"/iss_position/latitude","value":-4.891777641581724},
{"op":"replace","path":"/iss_position/longitude","value":137.61496558611873},
{"op":"replace","path":"/timestamp","value":1452186035}]

On the first call, the API send us the object iss_position, whereas on next calls, it will send us updates in the JSON-Patch format.

Once you have your token, you can add the Streamdata.io JavaScript SDK and a JSON-Patch library to your dev-dependencies in the bower.json file:
"devDependencies": {
"ionic": "driftyco/ionic-bower#1.3.2",
"streamdataio-js": "https://github.com/streamdataio/streamdataio-js-sdk.git#v1.0.5",
"fast-json-patch": "^1.1.1"
}

After running the bower install command to add them to your www/lib folder, declare them in your index.html file:
<script src="lib/streamdataio-js/dist/streamdataio.min.js"></script>
<script src="lib/fast-json-patch/dist/json-patch-duplex.min.js"></script>

Now we can open the streaming session in the MapCtrl in controllers.js. First, we need to create an EventSource object which takes your Streamdata.io App Token and the ISS API as parameters:
.controller('MapCtrl', function ($scope) {

...

var sdToken = "your-token";
var issApi = "http://api.open-notify.org/iss-now.json";
var myEventSource = streamdataio.createEventSource(issApi, sdToken);
}

 

Then we need to register to two callbacks: onData to retrieve the initial document, and onPatch to retrieve updates:
myEventSource.onData(function (snapshot) { // initialize your data with the initial snapshot
$scope.issPosition = snapshot;
});

myEventSource.onPatch(function (data) { // update the data with the provided patch
jsonpatch.apply($scope.issPosition, data);
});

 

Now that we retrieve the position, we need a method to update map's center and the ISS marker according to the new value:
function refreshMapAndISSMarker(){
var latLng = new google.maps.LatLng($scope.issPosition.iss_position.latitude,
$scope.issPosition.iss_position.longitude);
$scope.map.setCenter(latLng);
$scope.issMarker.setPosition(latLng);
}

 

Finally, we just need to call this method in our onData and onPatch methods and start the streaming session by calling the open method on the myEventSource object:
myEventSource.onData(function (snapshot) { // initialize your data with the initial snapshot
$scope.issPosition = snapshot;
refreshMapAndISSMarker();
});

myEventSource.onPatch(function (data) { // update the data with the provided patch
jsonpatch.apply($scope.issPosition, data);
refreshMapAndISSMarker();
});

myEventSource.open();

 

Et voilĂ ! Our map is now showing the ISS position in real-time :)

Screenshot Demo Iss.io

There are 0 comments.

Have something to say?

Drop a comment below.

Share - 'Show ISS current position using ionic' on Google Share - 'Show ISS current position using ionic' on Facebook Share - 'Show ISS current position using ionic' on Twitter Share - 'Show ISS current position using ionic' on Reddit Share - 'Show ISS current position using ionic' on LinkedIn