Google Maps are changing the way we see the world. Lets change the way we look at Google Maps
Google Maps are a perfect match when you are dealing with location based apps. In this article we will learn to integrate the Google Maps api and Google Places api in our Ionic App and look at the solution to long press issue that Ionic suffers from and how to resolve that.
Get Ionic running:
Make sure you have node.js installed on your system
$ sudo npm install -g cordova
$ sudo npm install -g ionic
Create a new Ionic Application:
ionic start googlePlacesDemo blank
cd googlePlacesDemo/
Get list of all the platforms added in your application:
ionic platform
you will see the list of all the installed platforms
Example :
Installed platforms: ios 3.8.0
Available platforms: amazon-fireos, android, blackberry10, browser, firefoxos, osx, webos
Add Android and iOS platforms to you app:
ionic platform add android
ionic platform add ios
Following plugins will be installed once any platform is added:
cordova-plugin-console
cordova-plugin-device
cordova-plugin-splashscreen
cordova-plugin-statusbar
cordova-plugin-whitelist
ionic-plugin-keyboard
ng-cordova is an AngularJs Service which integrates cordova plugins into IONIC Applications. Download ng-cordova.master.zip from here and place the ng-cordova.min.js in the www/js. ngCordova depends on AngularJS . In your index.html , place ng-cordova.min.js before cordova.js and after AngularJs/ Ionic files.
angular
.module('starter', ['ionic'])
.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('MapCtrl', [
'$scope',
function ($scope) {
function initialize() {
var mapOptions = {
center: { lat: 28.613939, lng: 77.209021 },
zoom: 13,
disableDefaultUI: true, // DISABLE MAP TYPE
scrollwheel: false,
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var input = /** @type {HTMLInputElement} */ (document.getElementById('pac-input'));
// Create the autocomplete helper, and associate it with
// an HTML text input box.
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
// Get the full place details when the user selects a place from the
// list of suggestions.
google.maps.event.addListener(autocomplete, 'place_changed', function () {
infowindow.close();
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
// Set the position of the marker using the place ID and location.
marker.setPlace(
/** @type {!google.maps.Place} */ ({
placeId: place.place_id,
location: place.geometry.location,
})
);
marker.setVisible(true);
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + 'Place ID: ' + place.place_id + '<br>' + place.formatted_address + '</div>');
infowindow.open(map, marker);
});
}
// Run the initialize function when the window has finished loading.
google.maps.event.addDomListener(window, 'load', initialize);
},
]);
All Set, you might assume. There is a problem though, you will have to long press the autocomplete option to actually get it selected. The issue is that Gmap dynamically adds elements that need the data-tap-disabled property, so you'll have to manually add the property after google has added these elements to the dom.
To get it working on the Android device you need to add the following function in your controller:
$scope.disableTap = function () {
var container = document.getElementsByClassName('pac-container');
angular.element(container).attr('data-tap-disabled', 'true');
var backdrop = document.getElementsByClassName('backdrop');
angular.element(backdrop).attr('data-tap-disabled', 'true');
angular.element(container).on('click', function () {
document.getElementById('pac-input').blur();
});
};