Monday 28 September 2015

Implement Google Maps Using Ionic Framework

Hi,

Here is a way by which we can implement Google Maps in Ionic framework

Run the following command for a blank Ionic project-

ionic start Map blank
cd Map
ionic platform add android

Since we are using maps, it is probably a good idea to add geolocation functionality to your application.  You can add the Apache Cordova Geolocation API by running the following command:

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-geolocation.git


We are going to be using the Google Maps JavaScript SDK, which requires us to have an API key for use in our application.  Go into your Google API Console and register a new Google Maps application.

When you have Google API key, crack open your www/index.html file because we need to add the JavaScript library to our project.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>
    <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/ionic.app.css" rel="stylesheet">
    -->
    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <!-- cordova script (this will be a 404 during development) -->
      <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">
  <ion-pane>
      <ion-header-bar class="bar-stable">
          <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-content >
       
      </ion-content>
  </ion-pane>
  <script src="http://maps.googleapis.com/maps/api/js?key=Your_API_key&sensor=true"></script>
  </body>
</html>

Please note that the Google Maps JavaScript library cannot be downloaded, so there will always be a small delay during the initial setup when you launch your application.



Now that the SDK is included we need to add some CSS for displaying it on the screen.  Open your www/css/style.css file and add the following code:

.scroll {
    height: 100%;
}
#map {
    width: 100%;
    height: 100%;
}

Open your www/app.js file because we want to add a controller that handles the Google Map.

var Map = angular.module('starter', ['ionic'])
.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) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
Map.controller('MapController', function($scope, $ionicLoading) {
    google.maps.event.addDomListener(window, 'load', function() {
        var myLatlng = new google.maps.LatLng(37.3000, -120.4833);
        var mapOptions = {
            center: myLatlng,
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
        navigator.geolocation.getCurrentPosition(function(pos) {
            map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
            var myLocation = new google.maps.Marker({
                position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
                map: map,
                title: "My Location"
            });
        });
        $scope.map = map;
    });
});


  We need to add some code to our www/index.html file for containing the map.


 <ion-content ng-controller="MapController">
          <div id="map" data-tap-disabled="true"></div>
      </ion-content>

Thanks. 

Friday 25 September 2015

Using camera in ionic project

Hi,

Here is a cordova plugin for open a camera in ionic project-

Run the following command-

cordova plugin add org.apache.cordova.camera 

Then your main.html file look like-

<ion-view view-title="Profile">
    <ion-content>
            <div class="align-center">
     
                    <div class="user_img" ng-click="capture()">
                      Click here
                    </div>
</div> 
    </ion-content>
</ion-view>

And controller.js file look like-

myapp.controller('ProfileCtrl', function ($scope, $cordovaCamera) {
    var options = {
        quality: 50,
       destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 150,
        targetHeight: 150,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        correctOrientation: true
    };
    $scope.capture = function () {
      $cordovaCamera.getPicture(options).then(function (imageData) {
            var image = document.getElementById('myImage');
            console.log("image",image)
            //image.src = "data:image/jpeg;base64," + imageData;
        }, function (err) {
            console.log("error",JSON.stringify(err))
        });
    }
});
 

Thanks


Friday 18 September 2015

Floating point number to integer number in angularjs

Hi,

Here is a way in angularjs by which you can change a floating point number to integer -

In your index.html

       <script>
angular.module('Example', [])
.controller('ExampleController',[$scope',function($scope){
$scope.val = 123.455;
}])

               
                  </script>



<div ng-controller="ExampleController">
<label>Enter number:<input ng-model='val'></label>
<br>
<br>
number in floating point format:<span id='number-default'>
{{val | number}}
</span>
<br>
number in integer format:<span>
{{val | number:0}}
</span>
<br>
Negative number:<span>
{{-val | number:4}}
</span>
<br>
</div>


Thanks 

Friday 11 September 2015

Plugins to be required to run an ionic app

Hi,

Following are the plugins, which are required to run an ionic app in mobile-

1)Plugin for email composer-

cordova plugin add https://github.com/jcjee/email-composer.git

2)Plugin for date picker-

cordova plugin add cordova-plugin-datepicker


3)Plugin for in app browser-

cordova plugin add cordova-plugin-inappbrowser


4) Plugin for splash screen-

cordova plugin add cordova-plugin-splashscreen

5)Plugin for status bar-

cordova plugin add cordova-plugin-statusbar

6)Plugin for white-list -

ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git

7)Plugin for Keyboard-

cordova plugin add com.ionic.keyboard

8)Plugin for console-

cordova plugin add org.apache.cordova.console

9)Plugin for device-

cordova plugin add org.apache.cordova.device


Thanks


Friday 4 September 2015

Ionic modal


                       In Ionic, modals are provided by the $ionicModal  directive.  Ionic’s modal allows creation from either a template string or a URL. 
Here in this example we are using a URL.

Modals are created with a scope. This can be used in simple examples to pass data. 

Example:-
For using the $ionicModal directive in our example following will be the code of index.html-


<ion-header-bar class="bar-energized">
<h1 class="title">
Contact Info
</h1>
<ion-content>
<div class="card" ng-controller='MainCtrl' ng-click="openModal()">
<div class="item item-divider">
{{contact.name}}
</div>
<div class="item item-text-wrap">
{{contact.info}}
</div>
</div>
</ion-content>


contact.html 

HTML page which we call by the $ionicModal-


<script id="contact.html" type="text/ng-template"> <div class="modal"> <ion-header-bar> <h1 class="title">Edit Contact</h1> </ion-header-bar> <ion-content> <div class="list"> <label class="item item-input"> <span class="input-label">Name</span> <input type="text" ng-model="contact.name"> </label> <label class="item item-input"> <span class="input-label">Info</span> <input type="text" ng-model="contact.info"> </label> </div> <button class="button button-full button-energized" ng-click="closeModal()">Done</button> </ion-content> </ </div></script>


Now our controller.js file look like-


app.controller('MainCtrl', function($scope, $ionicModal) { $scope.contact = { name: 'Demo', info: 'Tap anywhere on the card to open the modal' } $ionicModal.fromTemplateUrl('contact.html', { scope: $scope, animation: 'slide-in-up' }).then(function(modal) { $scope.modal = modal }) $scope.openModal = function() { $scope.modal.show() } $scope.closeModal = function() { $scope.modal.hide(); }; $scope.$on('$destroy', function() { $scope.modal.remove(); });})