Professional Documents
Culture Documents
Mobile GIS Application
Mobile GIS Application
Overview:
Ionic is open source HTML5 Mobile App Development Framework
targeted at building hybrid mobile apps. It provides tools and services for
building Mobile UI with native look and feel. Ionic framework needs native
wrapper to be able to run on mobile devices.
Since Ionic is built on top of AngularJS and Apache Cordova, you will need
to have basic knowledge about these technologies. You also need to be
familiar with HTML, CSS and JavaScript, if you want to understand all the
information provided.
Ionic Framework Features
Following are the most important features of Ionic:
AngularJS − Ionic is using AngularJS MVC architecture for building
rich single page applications optimized for mobile devices.
CSS components − With the native look and feel, these components
offer almost all elements that a mobile application needs. The
components’ default styling can be easily overridden to accommodate
your own designs.
JavaScript components − These components are extending CSS
components with JavaScript functionalities to cover all mobile elements
that cannot be done only with HTML and CSS.
Cordova Plugins − Apache Cordova plugins offer API needed for using
native device functions with JavaScript code.
Ionic CLI − This is NodeJS utility powered with commands for starting,
building, running and emulating Ionic applications.
Ionic View − Very useful platform for uploading, sharing and testing
your application on native devices.
Licence − Ionic is released under MIT license.
Install Ionic:
First, install Node.js. Then, install the latest Ionic command-line tools in your
terminal. You can download Node.js from Node JS website
(https://nodejs.org/en/ ). Open download page.
Page 1 of 25 Mohammed Mahmoud
Select the appropriate version for your Operating system and platform. Install
Node.js after download process finished.
To install ionic open the command prompt and type the following command.
You have to be connected.
npm install -g cordova ionic
You can create ionic application using one of Ionic application template blank,
tabs or sidemenu.
The Ionic Start command will create a folder named futureApp and setup
the Ionic files and folders.
The above command will open your app in the web browser. Google Chrome
provides the device mode functionality for mobile development testing.
Select your ionic project folder and then click on Select Folder button.
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
The world is your oyster.
<p>
If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a>
will be your guide.
</p>
</ion-content>
Change the content of <ion-title> tag to The Future University and remove
the content of <ion-content> tag and add a new <div> tag with “mapDiv” id
and set the height property to 100%. Your home page should look like the
following code.
<ion-content padding>
<div id="mapDiv" style="height:100%;"></div>
</ion-content>
When you open your application in the browser it should look like the
following screenshot.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Ionic App</title>
<meta name="viewport" content="viewport-fit=cover, width=device-width,
initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-
scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<!-- cordova.js required for cordova apps (remove if not needed) -->
<script src="cordova.js"></script>
</head>
<body>
<!-- Ionic's root component and where the app will load -->
<ion-app></ion-app>
</body>
</html>
Inside the head section add the following code to load google map api in your
application. To include google map API in your page you need google map
key. Go to google developer website and generate a key.
<script src="https://maps.googleapis.com/maps/api/js?key=setYourKeyHere"></script>
Inside the body section, add the following JavaScript code to load google map
in your page.
<script>
var map = new google.maps.Map(document.getElementById('mapDiv'), {
center: new google.maps.LatLng(15.4325, 32.4321), zoom:6
});
</script>
Save your updates and go to the browser, your application should look like
the following.
let’s walk through the process of creating and navigating to pages in our app.
Taking a look at src/app/app.html, we see this line of code.
<ion-nav [root]="rootPage"></ion-nav>
We see that rootPage is set to HomePage, so HomePage will be the first page
loaded in the nav controller. Let’s take a look at it.
<ion-header>
<ion-navbar>
<ion-title>
The Future University
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
let’s check out the home that we are importing. Inside the src/pages/home/
folder, go and open up home.ts.
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
You may have noticed that each page has its own folder that is named after
the page. Inside each folder, we also see a .html and a .scss file with the same
name. For example, inside of home/ we will find home.ts, home.html,
and home.scss. Although using this pattern is not required, it can be helpful to
keep things organized.
Below, we see the home class. This creates a Page - an Angular component
with all Ionic directives already provided, to be loaded using Ionics’s
navigation system. Notice that because Pages are meant to be loaded
dynamically, they don’t need to have a selector. However, the selector is
useful in order to override the default styles on a specific page
(see home.scss): All pages have both a class, and an associated template that’s
being compiled as well.
A gps folder will created automatically inside pages directory of your project.
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
GpsPage
],
Now inside home page let us to add a new button. We will use this button to
open gps page.
<ion-content padding>
<button ion-button full>Open GPS</button>
<div id="mapDiv" style="height:100%;"></div>
</ion-content>
now we will add click event for the button. In button tag add click event.
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
}
openGPS(){
this.navCtrl .push(GpsPage);
}
<ion-header>
<ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Longitude</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Latitude</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Accuracy</ion-label>
<ion-input type="text"></ion-input>
</ion-item>
</ion-list>
</ion-content>
The Geolocation API is the perfect companion for a maps application – maps
deal with locations, and the Geolocation API allows you to retrieve a user’s
location. If you are running this on a device, make sure to install the
Geolocation plugin by running:
Geolocation will also work directly through the browser though, as it is part
of the HTML5 API. Let us to add (click) event for Get Current Position
button in src/pages/gps/gps.html.
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Longitude</ion-label>
<ion-input type="text" #lng></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Latitude</ion-label>
<ion-input type="text" #lat></ion-input>
</ion-item>
<ion-item>
</ion-list>
Within the export class GpsPage add the input fields as objects. Moreover,
add geolocation object as a parameters to the constructor method.
@ViewChild('lng') lng;
@ViewChild('lat') lat;
@ViewChild('acc') acc;
getCoords(){
this.geo.getCurrentPosition().then((position)=>{
this.lng.value = position.coords.longitude;
this.lat.value = position.coords.latitude;
this.acc.value = position.coords.accuracy;
}).catch((error)=>{
console.log(error);
});
}
Save your application and use the ionic serve command to test your
application in browser. Click on GET CURRENT POSITION button.