Unable to select autocomplete results in google places

I use google places APIin my mobile application based on ionic / cordova, angular on Android platform.

I can get the search results from the value of the text field using the API, but when I select an option, just clicking on one of the search results, the text field remains empty.
But when I click and save the search parameter for 2-3 seconds, this parameter is selected.

It seems strange to me, but it happens.
place_changed eventdoes not work when pressed quickly.
I really don't know what could be causing this problem? I tried to Faskclick.jfix 300 ms, but it didn’t work, although I think this problem is not related to 300 ms delay.

My code is:

 function initialize() {
    var autocomplete = new google.maps.places.Autocomplete(input, options);
};   

Please help me here.

+4
source share
2 answers

I used ngMap and angular-google-places-autocomplete together in the following example.

Hope this helps.

angular.module('app', ['ionic', 'google.places', 'ngMap'])

.controller('MapCtrl', ['$scope', 'NgMap',
	function ($scope, NgMap) {

		$scope.location = null;

		NgMap.getMap().then(function (map) {
			console.log("getMap");
			$scope.map = map;
		});
		
		$scope.$on('g-places-autocomplete:select', function(event, place) {
			console.log('new location: ' + JSON.stringify(place));
			$scope.data = {
				lat: place.geometry.location.lat(),
				lng: place.geometry.location.lng()
			};
			$scope.map.setCenter(place.geometry.location);
			console.log($scope.data);
		});
	}
]);
.map {
  width: 100%;
  height: 500px !important;
}
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
  <title></title>

  <script src="https://maps.google.com/maps/api/js?libraries=visualization,drawing,geometry,places"></script>
  <link href="http://code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
  <script src="http://code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
  <script src="https://rawgit.com/kuhnza/angular-google-places-autocomplete/master/dist/autocomplete.min.js"></script>
  <link href="https://rawgit.com/kuhnza/angular-google-places-autocomplete/master/dist/autocomplete.min.css" rel="stylesheet">
  <link href="style.css" rel="stylesheet">
  
  <script src="app.js"></script>
</head>

<body ng-app="app" ng-controller="MapCtrl">
  <ion-pane>
<ion-header-bar class="bar-positive">
  <h1 class="title">Ionic map</h1>
</ion-header-bar>
<ion-content class="has-header padding">
  
  <div class="item item-input-inset">
      <label class="item-input-wrapper">
        <input type="text" g-places-autocomplete ng-model="location" placeholder="Insert address/location"/>
      </label>
      <button ng-click="location = ''" class="button ion-android-close input-button button-small" ng-show="location"></button>
  </div>
  <ng-map zoom="6" class="map">
    <marker position="{{data.lat}}, {{data.lng}}"></marker>
  </ng-map>
</ion-content>
  </ion-pane>
</body>

</html>
Run codeHide result
+2
source

I did not find my solution, but https://github.com/stephjang/placecomplete is a great plugin that works around :)

0
source

Source: https://habr.com/ru/post/1629683/


All Articles