Skip to content
This repository was archived by the owner on Nov 30, 2018. It is now read-only.

Commit f91d06a

Browse files
committed
Fixed bounds, center and zoom handling
Removed the requirement for bounds, center and zoom attributes on the ui-gmap-google-map directive. Now only the center or bounds attribute is required, and the zoom is optional (it defaults to 10). If only the bounds attribute is specified, the center lat/long is derived. Closes #1360.
1 parent c06eb50 commit f91d06a

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

src/coffee/directives/api/map.coffee

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ angular.module('uiGmapgoogle-maps.directives.api')
3939
</div><div ng-transclude style="display: none"></div></div>"""
4040

4141
scope:
42-
center: '=' # required
43-
zoom: '=' # required
42+
center: '=' # either bounds or center is required
43+
zoom: '=' # optional
4444
dragging: '=' # optional
4545
control: '=' # optional
4646
options: '=' # optional
4747
events: '=' # optional
4848
eventOpts: '=' # optional
4949
styles: '=' # optional
50-
bounds: '='
50+
bounds: '=' # either bounds or center is required
5151
update: '=' # optional
5252

5353
link: (scope, element, attrs) =>
@@ -59,12 +59,6 @@ angular.module('uiGmapgoogle-maps.directives.api')
5959
scope.map = null
6060

6161
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
6862

6963
uiGmapGoogleMapApi.then (maps) =>
7064
DEFAULTS = mapTypeId: maps.MapTypeId.ROADMAP
@@ -74,23 +68,49 @@ angular.module('uiGmapgoogle-maps.directives.api')
7468
instance: spawned.instance
7569
map: _gMap
7670

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'
8174
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'
8478
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+
85105
el = angular.element(element)
86106
el.addClass 'angular-google-map'
87107

88108
# Parse options
89109
opts =
90110
options: {}
91-
opts.options = scope.options if attrs.options
111+
opts.options = scope.options if attrs.options
92112

93-
opts.styles = scope.styles if attrs.styles
113+
opts.styles = scope.styles if attrs.styles
94114
if attrs.type
95115
type = attrs.type.toUpperCase()
96116
if google.maps.MapTypeId.hasOwnProperty(type)
@@ -120,7 +140,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
120140
if attrs.events and scope.events?.blacklist?
121141
scope.events.blacklist
122142
else []
123-
if _.isString disabledEvents
143+
if _.isString disabledEvents
124144
disabledEvents = [disabledEvents]
125145

126146
maybeHookToEvent = (eventName, fn, prefn) ->
@@ -141,14 +161,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
141161
scope.$evalAsync (s) ->
142162
s.dragging = dragging if s.dragging?
143163

144-
updateCenter = (c = _gMap.center, s = scope) ->
164+
updateCenter = (c = _gMap.center, s = scope) ->
145165
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()
152168

153169
settingFromDirective = false
154170
maybeHookToEvent 'idle', ->
@@ -157,7 +173,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
157173
sw = b.getSouthWest()
158174

159175
settingFromDirective = true
160-
scope.$evalAsync (s) ->
176+
scope.$evalAsync (s) ->
161177

162178
updateCenter()
163179

@@ -225,7 +241,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
225241
scope.$watch 'center', (newValue, oldValue) =>
226242
return if newValue == oldValue or settingFromDirective
227243
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()
229245

230246
unless dragging
231247
if !@validateCoords(newValue)
@@ -234,22 +250,22 @@ angular.module('uiGmapgoogle-maps.directives.api')
234250
_gMap.panTo coords
235251
else
236252
_gMap.setCenter coords
237-
238253
, true
239254

240255
zoomPromise = null
241256
scope.$watch 'zoom', (newValue, oldValue) ->
242257
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
244259
#make this time out longer than zoom_changes because zoom_changed should be done first
245260
#being done first should make scopes equal
246261
$timeout.cancel(zoomPromise) if zoomPromise?
247-
zoomPromise = $timeout ->
262+
zoomPromise = $timeout ->
248263
_gMap.setZoom newValue
249-
, scope.eventOpts?.debounce?.zoomMs + 20, false
264+
, scope.eventOpts?.debounce?.zoomMs + 20
265+
, false
250266

251267
scope.$watch 'bounds', (newValue, oldValue) ->
252-
return if newValue is oldValue
268+
return if newValue is oldValue
253269
if !newValue?.northeast?.latitude? or !newValue?.northeast?.longitude? or
254270
!newValue?.southwest?.latitude? or !newValue?.southwest?.longitude?
255271
$log.error "Invalid map bounds for new value: #{JSON.stringify newValue}"
@@ -261,10 +277,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
261277

262278
['options','styles'].forEach (toWatch) ->
263279
scope.$watch toWatch, (newValue,oldValue) ->
264-
return if _.isEqual(newValue,oldValue)
280+
return if _.isEqual(newValue,oldValue)
265281
if toWatch == 'options'
266282
opts.options = newValue
267283
else
268284
opts.options[toWatch] = newValue
269-
_gMap.setOptions opts if _gMap?
285+
_gMap.setOptions opts if _gMap?
270286
, true

src/coffee/directives/api/utils/gmap-util.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ angular.module('uiGmapgoogle-maps.directives.api.utils')
2626

2727
getCoords = (value) ->
2828
return unless value
29-
if Array.isArray(value) and value.length is 2
29+
if value instanceof google.maps.LatLng
30+
return value
31+
else if Array.isArray(value) and value.length is 2
3032
new google.maps.LatLng(value[1], value[0])
3133
else if angular.isDefined(value.type) and value.type is 'Point'
3234
new google.maps.LatLng(value.coordinates[1], value.coordinates[0])

0 commit comments

Comments
 (0)