@@ -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,28 @@ 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
75
+
76
+ # If center is not set, calculate the center point from bounds
77
+ if ! angular .isDefined (scope .center )
78
+ scope .center = new google.maps.LatLngBounds (@ getCoords (scope .bounds .southwest ), @ getCoords (scope .bounds .northeast )).getCenter ();
79
+
80
+ # If zoom is not set, use a default value
82
81
unless angular .isDefined (scope .zoom )
83
- $log . error ' angular-google-maps: map zoom property not set '
84
- return
82
+ scope . zoom = 10 ;
83
+
85
84
el = angular .element (element)
86
85
el .addClass ' angular-google-map'
87
86
88
87
# Parse options
89
88
opts =
90
89
options : {}
91
- opts .options = scope .options if attrs .options
90
+ opts .options = scope .options if attrs .options
92
91
93
- opts .styles = scope .styles if attrs .styles
92
+ opts .styles = scope .styles if attrs .styles
94
93
if attrs .type
95
94
type = attrs .type .toUpperCase ()
96
95
if google .maps .MapTypeId .hasOwnProperty (type)
@@ -120,7 +119,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
120
119
if attrs .events and scope .events ? .blacklist ?
121
120
scope .events .blacklist
122
121
else []
123
- if _ .isString disabledEvents
122
+ if _ .isString disabledEvents
124
123
disabledEvents = [disabledEvents]
125
124
126
125
maybeHookToEvent = (eventName , fn , prefn ) ->
@@ -141,14 +140,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
141
140
scope .$evalAsync (s) ->
142
141
s .dragging = dragging if s .dragging ?
143
142
144
- updateCenter = (c = _gMap .center , s = scope ) ->
143
+ updateCenter = (c = _gMap .center , s = scope ) ->
145
144
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 ()
145
+ s .center .latitude = c .lat () if s .center .latitude isnt c .lat ()
146
+ s .center .longitude = c .lng () if s .center .longitude isnt c .lng ()
152
147
153
148
settingFromDirective = false
154
149
maybeHookToEvent ' idle' , ->
@@ -157,7 +152,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
157
152
sw = b .getSouthWest ()
158
153
159
154
settingFromDirective = true
160
- scope .$evalAsync (s) ->
155
+ scope .$evalAsync (s) ->
161
156
162
157
updateCenter ()
163
158
@@ -225,7 +220,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
225
220
scope .$watch ' center' , (newValue , oldValue ) =>
226
221
return if newValue == oldValue or settingFromDirective
227
222
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 ()
223
+ return if coords .lat () is _gMap .center .lat () and coords .lng () is _gMap .center .lng ()
229
224
230
225
unless dragging
231
226
if ! @ validateCoords (newValue)
@@ -234,22 +229,22 @@ angular.module('uiGmapgoogle-maps.directives.api')
234
229
_gMap .panTo coords
235
230
else
236
231
_gMap .setCenter coords
237
-
238
232
, true
239
233
240
234
zoomPromise = null
241
235
scope .$watch ' zoom' , (newValue , oldValue ) ->
242
236
return unless newValue?
243
- return if _ .isEqual (newValue,oldValue) or _gMap ? .getZoom () == scope ? .zoom or settingFromDirective
237
+ return if _ .isEqual (newValue,oldValue) or _gMap ? .getZoom () == scope ? .zoom or settingFromDirective
244
238
# make this time out longer than zoom_changes because zoom_changed should be done first
245
239
# being done first should make scopes equal
246
240
$timeout .cancel (zoomPromise) if zoomPromise?
247
- zoomPromise = $timeout ->
241
+ zoomPromise = $timeout ->
248
242
_gMap .setZoom newValue
249
- , scope .eventOpts ? .debounce ? .zoomMs + 20 , false
243
+ , scope .eventOpts ? .debounce ? .zoomMs + 20
244
+ , false
250
245
251
246
scope .$watch ' bounds' , (newValue , oldValue ) ->
252
- return if newValue is oldValue
247
+ return if newValue is oldValue
253
248
if ! newValue ? .northeast ? .latitude ? or ! newValue ? .northeast ? .longitude ? or
254
249
! newValue ? .southwest ? .latitude ? or ! newValue ? .southwest ? .longitude ?
255
250
$log .error " Invalid map bounds for new value: #{ JSON .stringify newValue} "
@@ -261,10 +256,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
261
256
262
257
[' options' ,' styles' ].forEach (toWatch) ->
263
258
scope .$watch toWatch, (newValue ,oldValue ) ->
264
- return if _ .isEqual (newValue,oldValue)
259
+ return if _ .isEqual (newValue,oldValue)
265
260
if toWatch == ' options'
266
261
opts .options = newValue
267
262
else
268
263
opts .options [toWatch] = newValue
269
- _gMap .setOptions opts if _gMap?
264
+ _gMap .setOptions opts if _gMap?
270
265
, true
0 commit comments