1

Currently I have this script in my index.html inside the head tabs. I should move this to app.js, right? Either way, can you help me? How do I modify the script to fit in app.js? NOTE: I'm using angular.js so the app.js is an angular.module

    <script>
  function initialize() {
      var mapOptions = {
          zoom: 14,
          center: new google.maps.LatLng(59.3332346,18.0280576)
      };

      var map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);
  }

  function loadScript() {
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&' +
              'callback=initialize';
      document.body.appendChild(script);
  }

  window.onload = loadScript;

1 Answer 1

2

Move the loadScript into an actual <script> tag within the HEAD of your document. It is unnecessary to load the script using JavaScript.

Once you've done that create a directive for your map:

app.directive('map', function mapDirective() {

    return {

        restrict: 'A',
        scope: {
            options: '=map'
        },
        link: function link(scope, element) {
            new google.maps.Map(element[0], scope.options);
        }

    }

});

Setup your HTML document to utilise the directive:

<section ng-controller="MapController">
    <div data-map="options"></div>
</section>

And then finally create your controller to pass in the map options:

app.controller('MapController', function MapController($scope) {

    $scope.options = {
        zoom: 14,
        center: new google.maps.LatLng(59.3332346,18.0280576)
    }

});
Sign up to request clarification or add additional context in comments.

3 Comments

Wow thanks! Great answer! But I haven't gotten it to work yet. Furst I tried to implement all of it but then my app broke. So I started checking what's wrong and I don't know what exactly but as soon as I input the controller into controllers.js - the app breaks. It doesn't render. Even if I leave it empty like this: .controller('MapController', function MapController($scope) { }); What's wrong?
Sorry, I've modified the answer to include the new HTML. It's because we have an isolate scope and a controller on the same element, and each one is attempting to create a new scope.
Thanks but that's not it. I think the problem lies in the Ionic Framework starter app "Tabs". It comes with some sort of sate feature "Ionic uses AngularUI Router which uses the concept of states". I don't fully understand it and every time I try to add a control, directive whatever - the app breaks... So now I'm starting over with the ionic framework starter app "blank". When I'm back on track I'll try your code again. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.