Working with Ionic 2,Angular.js and cordova Geolocation

As suggested by some of our readers ,Today we are presenting new post for Ionic-2 with geolocation using angular.js

In this post for a sample we are going to build a weather app using the Forecast API by fetching current location.

Things we are going to do today is building a complete ionic app with side-menu, fetching data from API,asking for user location and much more..  

weather app with ionic and geolocaion

So,Let get Started!
Setting up and Creating a new Blank Project in Ionic:
Now let's create an app. From the terminal, go to your project folder. Type the following command to create a blank app with tabs.
Lets install ionic and cordova first,
npm install -g ionic cordova
Ionic framework now got installed on your machine,now lets create new blank project.
ionic start weatherapp blank

Simply replace tabs in the above line with tab or side menu to create a tab app, or an application with a side menu.
Finally navigate to the project directory and add iOS and/or Android platforms to Ionic and test the app.
cd weatherapp
ionic platform add ios/Android
ionic build ios/Android
ionic emulate ios/android

To complete the app we need to:

  1. Define the front end views
  2. Define controllers that provide data
  3. Define routes to tie the views to the controllers

Since we created a blank apps ,create a template folder and  Navigate to the folder www/templates from your application root directory. This folder contains template files that will be rendered for the app.
Creating template files which we will use for Side-menu and fetching and showing weather UI using Forecast API

A)Create new template/menus.html file and paste the below code to it.
This file contains the html code for creating side-menus in your ionic applications.
We are try to fetch and convert the temperature either to Celsius/Fahrenheit using the ionic-radio component.

    <ion-nav-bar class="bar-stable">
      <ion-nav-buttons side="right">
        <button class="button button-clear ion-navicon" menu-toggle="right"></button>
    <ion-nav-view name="menuContent">

  <ion-side-menu side="right">
    <ion-header-bar class="bar-stable">
      <h1 class="title">Settings</h1>
      <ion-item class="item-divider icon ion-home"> Home</ion-item>
      <ion-radio ng-model="settings.units" ng-value="'us'" menu-close="true" >Fahrenheit</ion-radio>
    <ion-radio ng-model="settings.units" ng-value="'si'" menu-close="true" >Celsius</ion-radio>
      <ion-item class="item-divider ">Cities</ion-item>
   <ion-item class="item-divider">Location</ion-item>


Next ,create the weather.html template which will fetch the weather details from Forecast API and showcase in UI.
The weather.html has the function of pull-to-refresh functionality which will be used to refresh the location.

<ion-view id="weatherContainer">
    <ion-refresher pulling-text="Pull to refresh..." on-refresh ="doRefresh()">
    <div id="current-weather" ng-show="haveData">
      <h1 class="current-temp">{{currentTemp}}&deg;</h1>
      <span id="temp-hi"><i class="icon ion-arrow-up-c"></i>{{highTemp}}&deg;</span>
      <span id="temp-lo"><i class="icon ion-arrow-down-c"></i>{{lowTemp}}&deg;
        </h3> <h3>
      <span id="temp-hi"><i class="icon ion-android-pin"></i>{{longi}}&deg;</span>
      <span id="temp-lo"><i class="icon ion-android-pin"></i>{{lati}}&deg;


Adding the template and main controller to index.html
Include the controller file and template file in the main index.hml to bind all the things at one. by creating three controller Navigate to js folder and create three js files. controller.js -handling Geolocation of the app by contacting the ngCordova plugin. app.js-Defining the routers and controller mapping service.js-Sending the request to forecast API and updating with response.

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>
    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
    <script src="js/app.js"></script>
  <body ng-app="app">


Here Comes the Controller!
Building the controller with angular.js that goes to the backend and does all the work

Navigate to js folder and add the below code to it.

angular.module('app', ['ionic', 'ngCordova', 'app.controllers'])
.constant('FORECASTIO_KEY', 'YOUR-API_KEY')//add your key here.
.config(function ($stateProvider, $urlRouterProvider) {
      .state('app', {
      url: '/app',
      abstract: true,
      templateUrl: 'templates/menu.html',
      controller: 'AppCtrl'

    .state('', {
      url: '/weather',
      views: {
        'menuContent': {
          templateUrl: 'templates/weather.html',
          controller: 'WeatherCtrl'

    // if none of the above states are matched, use this as the fallback
.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
    if(window.StatusBar) {

Creating the Controller.js which will be used to locate used using cordova geolocation and fetch the data from Forecast API.

Important Methods while working the geolocation:
timeout -Maximum length of time (milliseconds) that is allowed to pass
maximumAge-Accept a cached position whose age is no greater than the specified time in milliseconds
enableHighAccuracy-Provides a hint that the application needs the best possible results
Returns Object with user information, such as id, lastName


timeout-Maximum length of time (milliseconds) that is allowed to pass
maximumAge-Accept a cached position whose age is no greater than the specified time in milliseconds
enableHighAccuracy-Provides a hint that the application needs the best possible results
Returns Object - watchID which is used to clear watch later on.

watchID- Uses the watchID returned from watchPosition()

angular.module('app.controllers', [''])
  .controller('AppCtrl', function ($scope, $log, Settings) {
    $'AppCtrl Created');
    $scope.settings = Settings;
  .controller('WeatherCtrl', function ($scope, $log, $ionicLoading, $ionicPlatform, $cordovaGeolocation, Location, Weather, Settings) {
    $'WeatherCtrl Created');
    $ionicPlatform.ready(function () {
    if ( == 0) {
      var posOptions = {
        timeout: 10000,
        enableHighAccuracy: false
        .then(function (position) {
 = position.coords.latitude;
          Location.long = position.coords.longitude;
      }, function (err) {
          // error
    $scope.haveData = false;
      template: 'Loading...'

    function getWeather() {
      $scope.haveData = false;
        template: 'Loading...'
      Weather.getWeatherAtLocation(, Location.long).then(function (resp) {
        $scope.current =;
        $scope.highTemp = Math.ceil([0].temperatureMax);
        $scope.lowTemp = Math.floor([0].temperatureMin);
        $scope.currentTemp = Math.ceil($scope.current.temperature);
        $scope.haveData = true;
      }, function (error) {
        alert('Unable to get current conditions');


    $scope.doRefresh = function () {
    $scope.$watch(function () {
      return Settings.units
    }, function (newVal, oldVal) {
      if (newVal !== oldVal) {

Setting Up and Calling the Forecast API through services.js
This will build the URL for forecast API with API key which we generated earlier and authenticate the URL to send request to API.
angular.module('', [])
.factory('Settings', function () {
  var Settings = {
    units: 'us'
  return Settings;
.factory('Location', function ()  {
    var Location = {
      lat: 0,
      long: 0
    return Location;
.factory('Weather', function($http, $log, Settings, FORECASTIO_KEY) {
  $'Weather Factory');
  var url = '' + FORECASTIO_KEY + '/';

  return {
    getWeatherAtLocation: function(lat, lng) {
      return $http.jsonp(url + lat + ',' + lng + '?callback=JSON_CALLBACK&units='+Settings.units);
Run the Application Run your application by below command. Ionic serve

Whats next?

In the next post we may use Google Maps to geolocate and build and learn something exciting!

If you have some suggestion for post to be covered you are welcomed to contact!

Please share,like,tweet with other developers happy Coding!

Hey I'm Venkat
Developer, Blogger, Thinker and Data scientist. nintyzeros [at] I love the Data and Problem - An Indian Lives in US .If you have any question do reach me out via below social media