In my previous article, I showed you how to integrate Google Maps into your Ionic 2 application. This time, we will go a little bit further. By building on the top of my previous example, we will acquire device (smartphone or even your desktop browser) geolocation data eventually showing it on Google Maps.

Note: If this tutorial was helpful, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here, again leave me a comment below. I'm here to help you, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me to, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!

PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.


Table of Contents

Ionic 2 is causing you problems? Are you struggling to make it work? Trust me, I wasn’t in any better situation. If you require more information beyond the subject of this article you will probably find it in a list below. Take a look; if there’s a topic not covered there, leave me a comment and I’ll cover it.


Before we can begin, make sure you have everything preconfigured for Ionic 2.
You should have these:
  • Android Environment (or iOS if you’re working on a MacOS)
  • nodeJS
  • Ionic 2
  • Cordova
Find more information here: IONIC 2 | INSTALLATION GUIDE if you don’t have a previous Ionic 2 installation or if you have never read my previous articles on this topic.

1. Update Ionic & Cordova

You need to have a latest nodeJS version, without it, you’ll not be able to install/update Cordova and Ionic appropriately.
If you have a previous Ionic/Cordova installation make sure its up to date, older versions may not work with this tutorial. Though latest versions may also mess things up:
npm install -g cordova ionic@beta
or you can even do it like this:
npm update -g cordova ionic@beta
I will remove beta keyword once Ionic 2 reach RC status.
Warning: Ionic2 is still in alpha status so the final implementation may differ from this code. I will update this article if and when that happens.

2. Create A New Project

ionic start Ionic2CordovaGeolocationExample blank --v2
cd Ionic2CordovaGeolocationExample
Open app directory and remove all content from a directory called pages but don’t delete it. Somewhere below you’ll find a link to GitHub repo holding a working example (or you can copy past provided code). Download zipped project repo folder, copy app and www directory and past it into a newly created folder.
Warning: Since some of you never worked with Ionic CLI. From this point and further, every time I tell you to execute something, do that inside a project folder.
Add Android platform:
ionic platform add android
MacOS users can also add iOS platform:
ionic platform add ios

3. Install Cordova Geolocation Plugin

Install Cordova Geolocation Plugin:
cordova plugin add cordova-plugin-geolocation
cordova plugin add cordova-plugin-whitelist
Open www folder and add this security meta tag to index.html file:
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
Be careful, don’t do this step before you add designated platform(s) (Android/iOS).
GitHub - Geolocation GitHub - Whitelist
Install Whitelist plugin only if you’re using Cordova 4.0 +. Without it, you’ll receive “Application Error: There was a network error.” error.


First take a look at this embedded example:
Ionic 2 Page Navigation

Source Walkthrough

Before we go any further with Cordova Geolocation Plugin we first need to include Google Maps SDK library. This SDK is not necessary for Cordova Geolocation Plugin, but this example will not work without it. Go to the www directory and open index.html file. Add this line just above cordova.js line:
<script src=""></script>
It should look like this:
<script src=""></script>
<script src="cordova.js"></script>
<script src="build/js/app.bundle.js"></script>
If you have a link that looks like this:
<script src=""></script>
Remove sensor parameter, it’s no longer important, Google Maps library will throw an error if you leave it as it is. Error looks like this:
Google Maps API warning: SensorNotRequired:
Make sure you have installed Cordova Geolocation Plugin, the rest of code will not work without it.
If you’re using Cordova 4+ you will need to check if there’s a Whitelist plugin installed with your project. If you have it, add this security meta tag to your index.html HEAD content:
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">
We still have one major step before we start with this implementation. If you intend to use this code inside a mobile application it’s important to initialize it after platform (device) is ready (this step is not essential if you’re woking on a classic desktop application). For this, we will need to use Ionic Platform service. It’s a set of utility methods that can be used to retrieve the platform (device) ready state and several other information. It’s also important to understand that Ionic v2 has a somewhat different implementation of Platform service then v1.
First let’s import Platform service:
import {Page, Platform} from 'ionic/ionic';
Then inject it into MapPage class constructor:
constructor(platform: Platform) {
    this.platform = platform;
During this phase we’ll also initialize our map using this function:
Function content looks like this:
initializeMap() {

	this.platform.ready().then(() => {

		let locationOptions = {timeout: 10000, enableHighAccuracy: true};


			(position) => {

				let options = {
				  center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
				  zoom: 16,
				  mapTypeId: google.maps.MapTypeId.ROADMAP
				} = new google.maps.Map(document.getElementById("map_canvas"), options);

			(error) => {
			}, locationOptions


Continue to the next page

  • Nuatga

    I have tried this example but this is not working on android device what is the reason?

  • Suresh

    Is it possible to use this Plugin to check if the GPS Module is enabled on the device. Or is there any other plugin which i could use to check this.