30

I am very new to AngularJS and I am trying to use https://github.com/angular-ui/angular-google-maps.

I am just trying to get the map to render on my page but I am getting an error message and I don't know what it means. Any help with understanding this error message would be appreciated.

I've created a plunk here:

github.com/twdean/plunk

Here is the Error in the browser:

Error: [$compile:multidir] Multiple directives [googleMap, markers] asking for new/isolated scope on: <google-map center="center" draggable="true" zoom="zoom" markers="markers" mark-click="true" style="height: 400px"> 

http://errors.angularjs.org/1.2.3/$compile/multidir?p0=googleMap&p1=markers&p2=new%2Fisolated%20scope&p3=%3Cgoogle-map%20center%3D%22center%22%20draggable%3D%22true%22%20zoom%3D%2
Yiannis Tsimalis
  • 739
  • 2
  • 11
  • 23
tdean
  • 508
  • 1
  • 7
  • 15
  • Could you create a JSFiddle or Plunk? That would help. – Tyler Eich Dec 03 '13 at 16:23
  • I have created a Plunk here: https://github.com/twdean/plunk – tdean Dec 03 '13 at 20:06
  • 1
    I'm not actually seeing you do the recommended property definitions (via `angular.extend`) in your controller - says you need that for your directive to work properly: http://nlaplante.github.io/angular-google-maps/#!/usage – Suvi Vignarajah Dec 03 '13 at 21:06
  • 2
    The recommended properties are present; he used a different syntax in his controller. – Tyler Eich Dec 03 '13 at 21:31
  • Any luck with this? I've run into the same error... – ac360 Dec 04 '13 at 00:51
  • 4
    As of November 27, 2013, the [Github README](https://github.com/nlaplante/angular-google-maps/blob/master/README.md) says the documentation on the website is outdated. It might be best to wait for the updated documentation. – Tyler Eich Dec 04 '13 at 02:39
  • 1
    I found this video on youtube http://www.youtube.com/watch?v=RbxDLTfQc5g explaining that you don't need the extend syntax, as Tyler said the properties are there just written differently. – tdean Dec 04 '13 at 02:41
  • Thanks Tyler I missed that in the README. – tdean Dec 04 '13 at 02:45

5 Answers5

72

I suggest another alternative, ng-map.

I created a google maps angularjs directive called, ng-map, for my own project. This does not require any Javascript coding for simple functionality.

To Get Started

One. Download and include ng-map.js or ng-map.min.js

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="dist/ng-map.min.js"></script>`

Two. use map tag, and optionally, control, marker, and shape tags

<ng-map zoom="11" center="[40.74, -74.18]">
  <marker position="[40.74, -74.18]" />
  <shape name="circle" radius="400" center="[40.74,-74.18]" radius="4000" />
  <control name="overviewMap" opened="true" />
</ng-map>`  

Examples

To use it in your app, simply include 'ngMap' as dependency to your app.

var myApp = angular.module('myApp', ['ngMap']);

Hope it helps

-------------- EDIT -------------------

For those who looks for angular2 version,
there is also ng2-map https://ng2-ui.github.io/#/google-map

allenhwkim
  • 27,270
  • 18
  • 89
  • 122
  • @Farm, sorry for that. I made fixes – allenhwkim Jan 30 '14 at 01:23
  • excellent repo and helper lib. +1 here and star on github :-) – CharlieTuna Mar 10 '14 at 01:08
  • Does ngMap only handle hard-coded HTML? Or can I generate the HTML for it from a controller at run time? – Mawg says reinstate Monica Mar 11 '14 at 03:01
  • +1 but I don't understand. Isn't it the controller's job to get data from the model into the view? I am new to Angular (and JS) and don't know what you man by compile it. I want to get data from a database every few minutes and update my map. Can ngMap do that? – Mawg says reinstate Monica Mar 12 '14 at 02:23
  • @allenhwkim Ok, I found http://docs.angularjs.org/api/ng/service/$compile and it looks darn tricky for a newbie. Can I use $http to GET in the $compile and generate the ngMap tags from that? Did anyone ever try? Is there an example? – Mawg says reinstate Monica Mar 12 '14 at 05:54
  • 2
    @Mawg, if you don't change HTML but only change map, ngMap does the job. You don't need to do compile. You get the data from the server and communicate with ngMap using $scope.map, you can do it. I don't exactly what you want to do, so please make an example and make your own question. I will answer it gladly. – allenhwkim Mar 12 '14 at 16:07
  • @allenhwkim Could you please take a look at http://stackoverflow.com/questions/22372161/using-ng-repeat-with-markers-in-ngmap ? When we get it working, feel free to add to your site as an example. Thank a 1,000,000 for your help. – Mawg says reinstate Monica Mar 13 '14 at 07:45
  • Somehow, if I add ng-show to make the map visible later on, the map is not working properly. It then only shows a portion of the map. Anybody got a clue? – newman Sep 25 '14 at 19:11
  • @miliu, try this, https://github.com/allenhwkim/... If that one does not work, you need to resize the map manally by `google.maps.events(scope.map, "resize")` Search "resize" in the github issue tracker – allenhwkim Sep 25 '14 at 19:46
  • `$timeout(function () {google.maps.event.trigger($scope.map, 'resize');}, 500);` seems to fix the sizing issue. However, the marker doesn't show up anymore. Any fix? – newman Sep 25 '14 at 21:04
  • @miliu, that does not make sense. Please make a separate question with tag `angularjs-google-maps` and example. – allenhwkim Sep 25 '14 at 21:09
  • @allenhwkim though I am very late to this but thanks a bunch for your effort on google map lib for angularjs. I really loved that, but could you please direct me where i can find similar lib to implement infobox because I didn't find it on your lib, also if it isn't available in your lib then where can I get that lib/resource to implement with such ease as your's? – iCurious Mar 30 '15 at 16:58
  • @IConfused it has infoWindow, not infoBox. you can find examples in testapp diretory. – allenhwkim Mar 30 '15 at 17:02
  • @allenhwkim I do found infowindow on your site, but I need infobox that are more customizable than infowindow as my UI is quite complex for infowindow to handle. – iCurious Mar 31 '15 at 06:43
  • @IConfusedt Currently it supports all google official objects only at this time. You still can do raw programming with ngMap as you see in testapp directory with file name with 'raw'. – allenhwkim Mar 31 '15 at 13:36
  • @allenhwkim The example links are not working any more as of Nov 24 2015 – CherylG Nov 24 '15 at 21:16
  • Hi allenhwkim, I'm trying to use your library, but the map doesn't display. I follow [this example](http://ngmap.github.io/). I don't know what I'm missing. Could you please tell me the basic necessary things need to be done to load the map? Thanks. – Triet Doan Dec 11 '15 at 09:34
  • @AnhTriet there is README for that, https://github.com/allenhwkim/angularjs-google-maps#to-get-started. Also follow the plunker example. – allenhwkim Dec 11 '15 at 19:34
  • Please dont prefix it with ng. Its reserved for angular team. I was confused if its developed by angular team – Vatsal May 12 '16 at 19:17
  • Nice and simple! Do you know why the zoom controls might not be rendering correctly (They are there, but just white): http://screencast.com/t/NovEtf79yo – Rodney Aug 07 '16 at 12:09
  • Looks very cool but I get: Google Maps API error: MissingKeyMapError , on mobileAngular(1.3) – becker Sep 14 '16 at 20:37
  • OK just saw it after Jun22/2016 Google is asking for a key. My older keys I used for Android don't work so probably I need to go back and get new keys – becker Sep 14 '16 at 20:54
  • @becker You've to get an API key from google and then insert it as a parameter to your script src as follows Here are instructions on getting your API key (see Get an API key): https://developers.google.com/maps/documentation/javascript/adding-a-google-map#key – Sarah Sep 21 '17 at 15:23
  • @allenhwkim thanks for this. I was wondering is it possible to add an ng-click to a marker? I cant seem to get it working. also is it possible to add multiple markers? – Sarah Sep 21 '17 at 15:25
  • 1
    @Sarah no, marker is not a DOM object. yes for multiple-markers. – allenhwkim Sep 21 '17 at 20:19
8

It seems that you're including a "markers" attribute within your google-map element.

Indeed as @Tyler Eich mentions, the docs as of Dec 5, 2013 remain outdated. Removing the marker attribute seems to make it work.

Here is how to display markers:

index.html

<div ng-controller="MapCtrl">
    <google-map id="mymap" 
            center="center" 
            zoom="zoom" 
            draggable="true"                                    
            mark-click="false">
            <markers>
                <marker ng-repeat="marker in markers" coords="marker">
                </marker>
            </markers>
    </google-map>
</div>

controller.js

var controller = module.controller('MapCtrl', function($scope) {
  $scope.myMarkers = [
      {
         "latitude":33.22,
         "longitude":35.33
      },
      ...
  ];

  $scope.center = {
      latitude: 33.895497,
      longitude: 35.480347,
  };

  $scope.zoom = 13;
  $scope.markers = $scope.myMarkers;
  $scope.fit = true;
});
Community
  • 1
  • 1
Nicholas Credli
  • 907
  • 9
  • 14
4

I have just today had a need to implement a google map into my angular application. Since my implementation required something quite simple I decided to do this myself and create a simple directive. I will leave my source below for anyone who might find it usefull.

  1. Create a directive
  2. Create HTML
  3. Optionally pass the coordinates through directive/controller if you need.

DIRECTIVE

angular.module('ngPortalApp')
  .directive('googleMap', function () {
    return {
      template: '<iframe width="100%" height="350" frameborder="0" style="border:0"></iframe>',
      restrict: 'E',
      scope: {
        pbcode: '='
      },
      link: function postLink(scope, element) {
        var mapFrame = element.find("iframe");
          if (scope.pbcode) {
            mapFrame.attr('src', "https://www.google.com/maps/embed?pb=" + scope.pbcode);
          }
          else {
            mapFrame.attr('src', '');
          }
      }
    };
  });

HTML

<div class="col-lg-12">
<google-map pbcode="'!1m0!3m2!1sen!2srs!4v1445332712674!6m8!1m7!1s34JkuBgIzmIJNmpFNThuUg!2m2!1d40.75816449978445!2d-73.98911289129973!3f175.51693470959802!4f7.069842517148402!5f0.7820865974627469'"></google-map>
</div>

Thats all there is to it. Remember that the google query string is called "pb" this is the code used in all embed codes you can grab from google. You may pass this to your directive straight up or if you need to via your controller or even through the directive itself. You can set any iframe map settings in the directive within the template.

To change between Street View or Map View you just send the corresponding code whichever source you are getting them from (db, json, etc).

For the above example:

Street Code:

!1m0!3m2!1sen!2srs!4v1445332712674!6m8!1m7!1s34JkuBgIzmIJNmpFNThuUg!2m2!1d40.75816449978445!2d-73.98911289129973!3f175.51693470959802!4f7.069842517148402!5f0.7820865974627469

Map Code:

!1m18!1m12!1m3!1d755.5452773113641!2d-73.98946157079955!3d40.75804119870795!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0000000000000000%3A0x1952a6258d36ecc5!2sMcDonald's!5e0!3m2!1sen!2srs!4v1445332854795

Haven't had any issues with this as yet. Here's a fiddle: jsFiddle

geostima
  • 325
  • 2
  • 18
0

If you want to include markers you should do that in another element inside the google-maps directive. You should do something like:

<google-maps options="someOptionsObject"> 
<markers cords="someArrayWithLatitudeAndLongitudeCords"></markers>    
</google-maps>
PandAngular
  • 101
  • 3
0

For Angular2, there is angular2-google-maps. License as of 2016-05-14: MIT.

koppor
  • 19,079
  • 15
  • 119
  • 161