@@ -39,15 +39,15 @@ angular.module('uiGmapgoogle-maps.directives.api')
39
39
</div><div ng-transclude style="display: none"></div></div>"""
40
40
41
41
scope :
42
- center : ' =' # required
43
- zoom : ' =' # required
42
+ center : ' =' # either bounds or center is required
43
+ zoom : ' =' # optional
44
44
dragging : ' =' # optional
45
45
control : ' =' # optional
46
46
options : ' =' # optional
47
47
events : ' =' # optional
48
48
eventOpts : ' =' # optional
49
49
styles : ' =' # optional
50
- bounds : ' ='
50
+ bounds : ' =' # either bounds or center is required
51
51
update : ' =' # optional
52
52
53
53
link : (scope , element , attrs ) =>
@@ -59,12 +59,6 @@ angular.module('uiGmapgoogle-maps.directives.api')
59
59
scope .map = null
60
60
61
61
scope .idleAndZoomChanged = false
62
- unless scope .center ?
63
- unbindCenterWatch = scope .$watch ' center' , =>
64
- return unless scope .center
65
- unbindCenterWatch ()
66
- @ link scope, element, attrs # try again
67
- return
68
62
69
63
uiGmapGoogleMapApi .then (maps) =>
70
64
DEFAULTS = mapTypeId : maps .MapTypeId .ROADMAP
@@ -74,23 +68,49 @@ angular.module('uiGmapgoogle-maps.directives.api')
74
68
instance : spawned .instance
75
69
map : _gMap
76
70
77
- # Center property must be specified and provide lat &
78
- # lng properties
79
- if not @ validateCoords (scope .center )
80
- $log .error ' angular-google-maps: could not find a valid center property'
71
+ # Either a center or bounds lat/long property must be specified
72
+ if not angular .isDefined (scope .center ) and not angular .isDefined (scope .bounds )
73
+ $log .error ' angular-google-maps: a center or bounds property is required'
81
74
return
82
- unless angular .isDefined (scope .zoom )
83
- $log .error ' angular-google-maps: map zoom property not set'
75
+
76
+ if angular .isDefined (scope .center ) and not @ validateCoords (scope .center )
77
+ $log .error ' angular-google-maps: the supplied center property (' + scope .center + ' ) is not valid'
84
78
return
79
+
80
+ if angular .isDefined (scope .bounds )
81
+ if ! angular .isDefined (scope .bounds .northeast )
82
+ $log .error ' angular-google-maps: bounds.northeast property is not defined'
83
+ return
84
+
85
+ if ! angular .isDefined (scope .bounds .southwest )
86
+ $log .error ' angular-google-maps: bounds.southwest property is not defined'
87
+ return
88
+
89
+ if not @ validateCoords (scope .bounds .northeast )
90
+ $log .error ' angular-google-maps: bounds.northeast property (' + scope .bounds .northeast + ' ) is not valid'
91
+ return
92
+
93
+ if not @ validateCoords (scope .bounds .southwest )
94
+ $log .error ' angular-google-maps: bounds.southwest property (' + scope .bounds .southwest + ' ) is not valid'
95
+ return
96
+
97
+ # If center is not set, calculate the center point from bounds
98
+ if ! angular .isDefined (scope .center )
99
+ scope .center = new google.maps.LatLngBounds (@ getCoords (scope .bounds .southwest ), @ getCoords (scope .bounds .northeast )).getCenter ();
100
+
101
+ # If zoom is not set, use a default value
102
+ unless angular .isDefined (scope .zoom )
103
+ scope .zoom = 10 ;
104
+
85
105
el = angular .element (element)
86
106
el .addClass ' angular-google-map'
87
107
88
108
# Parse options
89
109
opts =
90
110
options : {}
91
- opts .options = scope .options if attrs .options
111
+ opts .options = scope .options if attrs .options
92
112
93
- opts .styles = scope .styles if attrs .styles
113
+ opts .styles = scope .styles if attrs .styles
94
114
if attrs .type
95
115
type = attrs .type .toUpperCase ()
96
116
if google .maps .MapTypeId .hasOwnProperty (type)
@@ -120,7 +140,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
120
140
if attrs .events and scope .events ? .blacklist ?
121
141
scope .events .blacklist
122
142
else []
123
- if _ .isString disabledEvents
143
+ if _ .isString disabledEvents
124
144
disabledEvents = [disabledEvents]
125
145
126
146
maybeHookToEvent = (eventName , fn , prefn ) ->
@@ -141,14 +161,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
141
161
scope .$evalAsync (s) ->
142
162
s .dragging = dragging if s .dragging ?
143
163
144
- updateCenter = (c = _gMap .center , s = scope ) ->
164
+ updateCenter = (c = _gMap .center , s = scope ) ->
145
165
return if _ .includes disabledEvents, ' center'
146
- if angular .isDefined (s .center .type )
147
- s .center .coordinates [1 ] = c .lat () if s .center .coordinates [1 ] isnt c .lat ()
148
- s .center .coordinates [0 ] = c .lng () if s .center .coordinates [0 ] isnt c .lng ()
149
- else
150
- s .center .latitude = c .lat () if s .center .latitude isnt c .lat ()
151
- s .center .longitude = c .lng () if s .center .longitude isnt c .lng ()
166
+ s .center .latitude = c .lat () if s .center .latitude isnt c .lat ()
167
+ s .center .longitude = c .lng () if s .center .longitude isnt c .lng ()
152
168
153
169
settingFromDirective = false
154
170
maybeHookToEvent ' idle' , ->
@@ -157,7 +173,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
157
173
sw = b .getSouthWest ()
158
174
159
175
settingFromDirective = true
160
- scope .$evalAsync (s) ->
176
+ scope .$evalAsync (s) ->
161
177
162
178
updateCenter ()
163
179
@@ -225,7 +241,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
225
241
scope .$watch ' center' , (newValue , oldValue ) =>
226
242
return if newValue == oldValue or settingFromDirective
227
243
coords = @ getCoords scope .center # get scope.center to make sure that newValue is not behind
228
- return if coords .lat () is _gMap .center .lat () and coords .lng () is _gMap .center .lng ()
244
+ return if coords .lat () is _gMap .center .lat () and coords .lng () is _gMap .center .lng ()
229
245
230
246
unless dragging
231
247
if ! @ validateCoords (newValue)
@@ -234,22 +250,22 @@ angular.module('uiGmapgoogle-maps.directives.api')
234
250
_gMap .panTo coords
235
251
else
236
252
_gMap .setCenter coords
237
-
238
253
, true
239
254
240
255
zoomPromise = null
241
256
scope .$watch ' zoom' , (newValue , oldValue ) ->
242
257
return unless newValue?
243
- return if _ .isEqual (newValue,oldValue) or _gMap ? .getZoom () == scope ? .zoom or settingFromDirective
258
+ return if _ .isEqual (newValue,oldValue) or _gMap ? .getZoom () == scope ? .zoom or settingFromDirective
244
259
# make this time out longer than zoom_changes because zoom_changed should be done first
245
260
# being done first should make scopes equal
246
261
$timeout .cancel (zoomPromise) if zoomPromise?
247
- zoomPromise = $timeout ->
262
+ zoomPromise = $timeout ->
248
263
_gMap .setZoom newValue
249
- , scope .eventOpts ? .debounce ? .zoomMs + 20 , false
264
+ , scope .eventOpts ? .debounce ? .zoomMs + 20
265
+ , false
250
266
251
267
scope .$watch ' bounds' , (newValue , oldValue ) ->
252
- return if newValue is oldValue
268
+ return if newValue is oldValue
253
269
if ! newValue ? .northeast ? .latitude ? or ! newValue ? .northeast ? .longitude ? or
254
270
! newValue ? .southwest ? .latitude ? or ! newValue ? .southwest ? .longitude ?
255
271
$log .error " Invalid map bounds for new value: #{ JSON .stringify newValue} "
@@ -261,10 +277,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
261
277
262
278
[' options' ,' styles' ].forEach (toWatch) ->
263
279
scope .$watch toWatch, (newValue ,oldValue ) ->
264
- return if _ .isEqual (newValue,oldValue)
280
+ return if _ .isEqual (newValue,oldValue)
265
281
if toWatch == ' options'
266
282
opts .options = newValue
267
283
else
268
284
opts .options [toWatch] = newValue
269
- _gMap .setOptions opts if _gMap?
285
+ _gMap .setOptions opts if _gMap?
270
286
, true
0 commit comments