I am new to IONIC. I am trying to use google places api in my ionic app. I am able to get the places via Google Places api. But the problem is I have to long press the autocomplete option in the app or else the selection is not reflected in the app.
Here is my Html code.
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box" data-tap-disabled="true">
<div id="map" data-tap-disabled="true"></div>
And this is my MapController.js
.controller('MapCtrl', function($scope, $rootScope, $state, $cordovaGeolocation, $ionicLoading, Markers, $ionicPopup, $location, SearchAll, shareData, constants) {
initAutocomplete();
function initAutocomplete() {
console.log("in Map Autocomplete");
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 19.075984, lng: 72.877656 },
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
// map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
console.log("in searchBox Listner");
if (places.length == 0) {
return;
}
console.log("Places : ", places);
console.log("LAT : ", places[0].geometry.location.lat());
console.log("LAONG : ", places[0].geometry.location.lng());
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
// [END region_getplaces]
}
});
The solution for this problem is discussed at https://github.com/driftyco/ionic/issues/1798. The issue is that google 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.
$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');
// leave input field if google-address-entry is selected
angular.element(container).on("click", function () {
document.getElementById('pac-input').blur();
});
};
now add ng-change='disableTap()'
to your input
:
<input ng-change='disableTap()' id="pac-input" class="controls" type="text" placeholder="Search Box" data-tap-disabled="true">
The github link uses ng-focus
but ng-change
worked better for me because sometimes the generated elements didn't exist yet by the time focus was fired.