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

Commit cbd17a3

Browse files
committed
Merge pull request #1748 from jbertouch/center-bounds-fix
Fixed bounds, center and zoom handling
2 parents c06eb50 + 55c0f61 commit cbd17a3

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/coffee/directives/api/map.coffee

Lines changed: 29 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,28 @@ 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
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
8281
unless angular.isDefined(scope.zoom)
83-
$log.error 'angular-google-maps: map zoom property not set'
84-
return
82+
scope.zoom = 10;
83+
8584
el = angular.element(element)
8685
el.addClass 'angular-google-map'
8786

8887
# Parse options
8988
opts =
9089
options: {}
91-
opts.options = scope.options if attrs.options
90+
opts.options = scope.options if attrs.options
9291

93-
opts.styles = scope.styles if attrs.styles
92+
opts.styles = scope.styles if attrs.styles
9493
if attrs.type
9594
type = attrs.type.toUpperCase()
9695
if google.maps.MapTypeId.hasOwnProperty(type)
@@ -120,7 +119,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
120119
if attrs.events and scope.events?.blacklist?
121120
scope.events.blacklist
122121
else []
123-
if _.isString disabledEvents
122+
if _.isString disabledEvents
124123
disabledEvents = [disabledEvents]
125124

126125
maybeHookToEvent = (eventName, fn, prefn) ->
@@ -141,14 +140,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
141140
scope.$evalAsync (s) ->
142141
s.dragging = dragging if s.dragging?
143142

144-
updateCenter = (c = _gMap.center, s = scope) ->
143+
updateCenter = (c = _gMap.center, s = scope) ->
145144
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()
152147

153148
settingFromDirective = false
154149
maybeHookToEvent 'idle', ->
@@ -157,7 +152,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
157152
sw = b.getSouthWest()
158153

159154
settingFromDirective = true
160-
scope.$evalAsync (s) ->
155+
scope.$evalAsync (s) ->
161156

162157
updateCenter()
163158

@@ -225,7 +220,7 @@ angular.module('uiGmapgoogle-maps.directives.api')
225220
scope.$watch 'center', (newValue, oldValue) =>
226221
return if newValue == oldValue or settingFromDirective
227222
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()
229224

230225
unless dragging
231226
if !@validateCoords(newValue)
@@ -234,22 +229,22 @@ angular.module('uiGmapgoogle-maps.directives.api')
234229
_gMap.panTo coords
235230
else
236231
_gMap.setCenter coords
237-
238232
, true
239233

240234
zoomPromise = null
241235
scope.$watch 'zoom', (newValue, oldValue) ->
242236
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
244238
#make this time out longer than zoom_changes because zoom_changed should be done first
245239
#being done first should make scopes equal
246240
$timeout.cancel(zoomPromise) if zoomPromise?
247-
zoomPromise = $timeout ->
241+
zoomPromise = $timeout ->
248242
_gMap.setZoom newValue
249-
, scope.eventOpts?.debounce?.zoomMs + 20, false
243+
, scope.eventOpts?.debounce?.zoomMs + 20
244+
, false
250245

251246
scope.$watch 'bounds', (newValue, oldValue) ->
252-
return if newValue is oldValue
247+
return if newValue is oldValue
253248
if !newValue?.northeast?.latitude? or !newValue?.northeast?.longitude? or
254249
!newValue?.southwest?.latitude? or !newValue?.southwest?.longitude?
255250
$log.error "Invalid map bounds for new value: #{JSON.stringify newValue}"
@@ -261,10 +256,10 @@ angular.module('uiGmapgoogle-maps.directives.api')
261256

262257
['options','styles'].forEach (toWatch) ->
263258
scope.$watch toWatch, (newValue,oldValue) ->
264-
return if _.isEqual(newValue,oldValue)
259+
return if _.isEqual(newValue,oldValue)
265260
if toWatch == 'options'
266261
opts.options = newValue
267262
else
268263
opts.options[toWatch] = newValue
269-
_gMap.setOptions opts if _gMap?
264+
_gMap.setOptions opts if _gMap?
270265
, 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)