Ionic (or AngularJS) blocks google maps autocomplete -- [Question Asked]

Query asked by user

I’m just trying to add a simple autocomplete form to my Ionic app.
So first I tried this, it works fine.
I then tried in my app (in the browser first).

I put this in my controller, and called it with ng-init=”initMaps()”

$scope.initMaps = function() {
  var center = new google.maps.LatLng(51.514032, -0.128383);
  var circle = new google.maps.Circle({
    center: center,
    radius: 50
  });
  var options = {componentRestrictions: {country: 'uk'}, types: ['geocode']}
  var input = document.getElementById('autocomplete');
  var autocomplete = new google.maps.places.Autocomplete(input, options);
  autocomplete.setBounds(circle.getBounds());
  console.log("maps loaded")
}

When I look at my console, I can see “maps loaded”, and google warnings like:

Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required

I just added this line in my template form:

 <input id="autocomplete" type="text" style="width: 200px;">

So, I think on a non-ionic (or angular) project it would work fine. But here, when I enter some letters, I don’t get any result

Does Angular/Ionic create some interference here ? Am I forced to install other modules ?

Thanks !

Answer we found from sources

Since Place Autocomplete is a part of Google Maps Places Library you probably forgot to include the loading of this library via libraries parameter, for example:

<script src="//maps.googleapis.com/maps/api/js?key={KEY}&libraries=places"></script>

Example

angular.module('ionic.example', ['ionic'])

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


        $scope.initMap = function () {

            var center = new google.maps.LatLng(51.514032, -0.128383);
            var circle = new google.maps.Circle({
                center: center,
                radius: 50
            });
           
            var options = { componentRestrictions: { country: 'uk' }, types: ['geocode'] }
            var input = document.getElementById('autocomplete');
            var autocomplete = new google.maps.places.Autocomplete(input, options);
            autocomplete.setBounds(circle.getBounds());

        }

        google.maps.event.addDomListener(window, 'load', $scope.initMap);

    });
.controls {
        margin-top: 10px;
        border: 1px solid transparent;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        height: 32px;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
      }
 <script src="//maps.googleapis.com/maps/api/js?libraries=places"></script>
<link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
<script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
<div ng-app="ionic.example" ng-controller="MapCtrl">
    <ion-header-bar class="bar-dark" >
      <h1 class="title">Map</h1>
    </ion-header-bar>
    <ion-content>
      <div id="map" data-tap-disabled="true"></div>
      <input id="autocomplete" placeholder="Enter your address" class="controls" type="text"></input>
    </ion-content>
 </div>

Codepen

Answered By – Vadim Gremyachev

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0


What is Angular?

Angular is an open-source, JavaScript outline written in TypeScript. Google keeps up with it, and its basic role is to foster single-page activities. As an edge, Angular enjoys clear benefits while likewise outfitting a standard design for formulators to work with. It empowers stoners to deliver huge tasks in a viable way. textures overall lift web improvement viability and execution by outfitting an agreeable construction so that formulators do n't need to continue to modify regulation from scratch. textures are efficient devices that offer formulators a large group of extra elements that can be added to programming easily.

However, is JavaScript ideal for creating single-sprinter activities that bear particularity, testability, and trend-setter efficiency? maybe not.

JavaScript is the most by and large utilized client-side prearranging language. It's composed into HTML reports to empower relations with web sprinters in endless extraordinary ways. As a genuinely simple to-learn language with inescapable help, creating current operations is appropriate.

Nowadays, we have various textures and libraries intended to give essential outcomes. As for front end web advancement, Angular addresses incalculable, while possibly not all, of the issues formulators face while utilizing JavaScript all alone.
Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0