-
-
Notifications
You must be signed in to change notification settings - Fork 708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
If no enterprises have been geocoded yet make sure Open Street Map displays correctly #5680
Merged
luisramos0
merged 5 commits into
openfoodfoundation:master
from
cillian:prevent-blank-open-street-map
Aug 5, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c40697c
If no enterprises have been geocoded yet make sure Open Street Map st…
cillian 1199a35
Extract out a JS service for calculating where to centre the map when…
cillian cc317bc
Move the :initialLatitude and :initialLongitude methods from the Open…
cillian 702669e
Merge branch 'master' into prevent-blank-open-street-map
cillian e6ab2ae
Remove unused positiveAngles and negativeAngles arrays from map centr…
cillian File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/assets/javascripts/darkswarm/services/map_centre_calculator.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Darkswarm.factory 'MapCentreCalculator', (Enterprises, openStreetMapConfig) -> | ||
new class MapCentreCalculator | ||
|
||
initialLatitude: => | ||
if Enterprises.geocodedEnterprises().length > 0 | ||
@_calculate("latitude", Enterprises.geocodedEnterprises()) | ||
else | ||
openStreetMapConfig.open_street_map_default_latitude | ||
|
||
initialLongitude: => | ||
if Enterprises.geocodedEnterprises().length > 0 | ||
@_calculate("longitude", Enterprises.geocodedEnterprises()) | ||
else | ||
openStreetMapConfig.open_street_map_default_longitude | ||
|
||
_calculate: (angleName, coordinates) => | ||
angles = [] | ||
|
||
for coordinate in coordinates | ||
angles.push(coordinate[angleName]) | ||
|
||
minimumAngle = Math.min.apply(null, angles) | ||
maximumAngle = Math.max.apply(null, angles) | ||
|
||
distanceBetweenMinimumAndMaximum = if maximumAngle > minimumAngle | ||
maximumAngle - minimumAngle | ||
else | ||
minimumAngle - maximumAngle | ||
|
||
minimumAngle + (distanceBetweenMinimumAndMaximum / 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
spec/javascripts/unit/darkswarm/services/map_centre_calculator_spec.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
describe 'MapCentreCalculator service', -> | ||
MapCentreCalculator = null | ||
Enterprises = null | ||
defaultLongitude = null | ||
defaultLatitude = null | ||
|
||
beforeEach -> | ||
module 'Darkswarm' | ||
defaultLongitude = -6 | ||
defaultLatitude = 53 | ||
angular.module('Darkswarm').value 'openStreetMapConfig', { | ||
open_street_map_default_latitude: 76.26, | ||
open_street_map_default_longitude: -42.66 | ||
} | ||
|
||
inject (_MapCentreCalculator_, _Enterprises_)-> | ||
MapCentreCalculator = _MapCentreCalculator_ | ||
Enterprises = _Enterprises_ | ||
|
||
describe "initialLatitude", -> | ||
it "calculates the center latitude of any present geocoded enterprises", -> | ||
Enterprises.geocodedEnterprises = -> [ | ||
{ latitude: 53, longitude: defaultLongitude }, | ||
{ latitude: 54, longitude: defaultLongitude } | ||
] | ||
|
||
expect(MapCentreCalculator.initialLatitude()).toEqual 53.5 | ||
|
||
it "returns the default configured latitude when there are no geocoded enterprises present", -> | ||
Enterprises.geocodedEnterprises = -> [] | ||
|
||
expect(MapCentreCalculator.initialLatitude()).toEqual 76.26 | ||
|
||
describe "initialLongitude", -> | ||
it "calculates the center longitude of any present geocoded enterprises", -> | ||
Enterprises.geocodedEnterprises = -> [ | ||
{ latitude: defaultLatitude, longitude: -6 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator.initialLongitude()).toEqual -6.5 | ||
|
||
it "returns the default configured longitude when there are no geocoded enterprises present", -> | ||
Enterprises.geocodedEnterprises = -> [] | ||
|
||
expect(MapCentreCalculator.initialLongitude()).toEqual -42.66 | ||
|
||
describe "_calculate", -> | ||
it "calculates the average angle correctly when given a single angle", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -7 | ||
|
||
it "calculates the centre correctly when given a set of positive angles", -> | ||
coordinates = [ | ||
{ latitude: 53, longitude: defaultLongitude }, | ||
{ latitude: 54, longitude: defaultLongitude } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("latitude", coordinates)).toEqual 53.5 | ||
|
||
it "calculates the centre correctly when given a set of negative angles", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: -6 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -6.5 | ||
|
||
it "calculates the centre correctly when given a mixture of positive and negative angles and the centre is positive", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: 7 }, | ||
{ latitude: defaultLatitude, longitude: -4 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual 1.5 | ||
|
||
it "calculates the centre correctly when given a mixture of positive and negative angles and the centre is negative", -> | ||
coordinates = [ | ||
{ latitude: defaultLatitude, longitude: 4 }, | ||
{ latitude: defaultLatitude, longitude: -7 } | ||
] | ||
|
||
expect(MapCentreCalculator._calculate("longitude", coordinates)).toEqual -1.5 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where's this
L
variable coming from? I can't see where it's set...?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That
L
is the Leaflet JS library, it gets included here https://github.com/openfoodfoundation/openfoodnetwork/blob/master/app/assets/javascripts/darkswarm/all.js.coffee#L11