From f96144a3841f2917cb3cc4f7e1029f9d4d988476 Mon Sep 17 00:00:00 2001 From: Sayre Blades Date: Mon, 6 Jan 2014 11:48:46 -0500 Subject: [PATCH 1/3] angular-ui-map doesnt play well with angular-ui-router. This change delays constrution of the google map until the page has been loaded. --- README.md | 7 +++++++ src/ui-map.js | 27 +++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2569b8b..8953745 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,13 @@ To see something it's better to add some CSS, like .map-canvas { height: 400px; } ``` +To delay the map construction until the page is visible, you can set the optional *ui-map-load-event* attribute. Here I have delayed google map construction until the ui-router's $viewContentLoaded event has been fired. This is required when using this component with ui-router. + +```html +
+
+
+``` ## Options [google.maps.MapOptions object](https://developers.google.com/maps/documentation/javascript/reference#MapOptions) can be passed through the main directive attribute`ui-map`. diff --git a/src/ui-map.js b/src/ui-map.js index 7df3e36..e625937 100644 --- a/src/ui-map.js +++ b/src/ui-map.js @@ -32,14 +32,29 @@ restrict: 'A', //doesn't work as E for unknown reason link: function (scope, elm, attrs) { - var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions)); - var map = new window.google.maps.Map(elm[0], opts); - var model = $parse(attrs.uiMap); + // this function will display the map + var displayMap = function() { + var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions)); + var map = new window.google.maps.Map(elm[0], opts); + var model = $parse(attrs.uiMap); + + //Set scope variable for the map + model.assign(scope, map); - //Set scope variable for the map - model.assign(scope, map); + bindMapEvents(scope, mapEvents, map, elm); + } - bindMapEvents(scope, mapEvents, map, elm); + // retrieve the optional ui-map-load-event: + // this is to delay the construction of the map until the div containing + // it is visible. In the case of angular-ui-router, this event can be + // set to "$viewContentLoaded" + var mapLoadEvent = attrs.uiMapLoadEvent; + if(mapLoadEvent){ + scope.$on(mapLoadEvent, displayMap); + } else { + displayMap(); + } + } }; }]); From 7d9561c6b2805fc8cb3469836bf15d8854a5fcba Mon Sep 17 00:00:00 2001 From: Sayre Blades Date: Tue, 7 Jan 2014 08:04:55 -0500 Subject: [PATCH 2/3] fix jshint warning --- src/ui-map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui-map.js b/src/ui-map.js index e625937..427de59 100644 --- a/src/ui-map.js +++ b/src/ui-map.js @@ -42,7 +42,7 @@ model.assign(scope, map); bindMapEvents(scope, mapEvents, map, elm); - } + }; // retrieve the optional ui-map-load-event: // this is to delay the construction of the map until the div containing From 380a7f345d43c69a00a2da1a3e4b93726c46f0a1 Mon Sep 17 00:00:00 2001 From: Sayre Blades Date: Fri, 14 Feb 2014 13:57:03 -0500 Subject: [PATCH 3/3] added ui-map-loaded event to signal the map has been displayed --- src/ui-map.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ui-map.js b/src/ui-map.js index 427de59..f2c0192 100644 --- a/src/ui-map.js +++ b/src/ui-map.js @@ -42,6 +42,7 @@ model.assign(scope, map); bindMapEvents(scope, mapEvents, map, elm); + scope.$emit('ui-map-loaded'); }; // retrieve the optional ui-map-load-event: