Skip to content

Commit e7cdd26

Browse files
committed
Fixed openNearest setting not opening correct location with custom sorting (sortBy) enabled
1 parent 55751f0 commit e7cdd26

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

dist/assets/js/plugins/storeLocator/jquery.storelocator.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -930,15 +930,20 @@
930930
* Location distance sorting function
931931
*
932932
* @param locationsarray {array} locationset array
933+
* @param distanceOverride {boolean} Force sort by distance
933934
*/
934-
sortNumerically: function (locationsarray) {
935+
sortNumerically: function (locationsarray, distanceOverride) {
935936
this.writeDebug('sortNumerically',arguments);
936937
var property = (
937938
this.settings.sortBy !== null &&
938939
this.settings.sortBy.hasOwnProperty('prop') &&
939940
typeof this.settings.sortBy.prop !== 'undefined'
940941
) ? this.settings.sortBy.prop : 'distance';
941942

943+
if (typeof distanceOverride !== 'undefined' && distanceOverride === true) {
944+
property = 'distance';
945+
}
946+
942947
if (this.settings.sortBy !== null && this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
943948
locationsarray.sort(function (a, b) {
944949
return ((b[property] < a[property]) ? -1 : ((b[property] > a[property]) ? 1 : 0));
@@ -2470,7 +2475,7 @@
24702475
_this.settings.callbackNearestLoc.call(this, _this.map, nearestLoc, infowindow, storeStart, page);
24712476
}
24722477

2473-
var markerId = 0;
2478+
var markerId = (nearestLoc.hasOwnProperty('markerid')) ? nearestLoc.markerid : 0;
24742479
var selectedMarker = markers[markerId];
24752480

24762481
_this.createInfowindow(selectedMarker, 'left', infowindow, storeStart, page);
@@ -2864,6 +2869,23 @@
28642869

28652870
// Sorting
28662871
if (_this.settings.sortBy !== null && typeof _this.settings.sortBy === 'object') {
2872+
2873+
// Sort the multi-dimensional array by distance to get the nearest location first when enabled
2874+
if (_this.settings.openNearest === true) {
2875+
this.sortNumerically(locationset, true);
2876+
2877+
// Save the closest location to a variable for openNearest setting
2878+
if (typeof locationset[0] !== 'undefined') {
2879+
2880+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
2881+
nearestLoc = locationset[locationset.length - 1];
2882+
} else {
2883+
nearestLoc = locationset[0];
2884+
}
2885+
}
2886+
}
2887+
2888+
// Custom sorting
28672889
_this.sortCustom(locationset);
28682890
} else {
28692891
// Sort the multi-dimensional array by distance
@@ -2891,7 +2913,7 @@
28912913
}
28922914
}
28932915

2894-
// Save the closest location to a variable for openNearest setting.
2916+
// Save the closest location to a variable for openNearest setting
28952917
if (typeof locationset[0] !== 'undefined') {
28962918
nearestLoc = locationset[0];
28972919
}
@@ -3029,7 +3051,7 @@
30293051
_this.settings.callbackMapSet.call(this, _this.map, originPoint, originalZoom, myOptions);
30303052
}
30313053

3032-
// Initialize the infowondow
3054+
// Initialize the infowindow
30333055
if ( typeof InfoBubble !== 'undefined' && _this.settings.infoBubble !== null ) {
30343056
var infoBubbleSettings = _this.settings.infoBubble;
30353057
infoBubbleSettings.map = _this.map;
@@ -3039,7 +3061,6 @@
30393061
infowindow = new google.maps.InfoWindow();
30403062
}
30413063

3042-
30433064
// Add origin marker if the setting is set
30443065
_this.originMarker(_this.map, origin, originPoint);
30453066

@@ -3068,6 +3089,10 @@
30683089
marker = _this.createMarker(point, locationset[y].name, locationset[y].address, letter, _this.map, locationset[y].category);
30693090
marker.set('id', y);
30703091
markers[y] = marker;
3092+
3093+
// Add marker ID to location data
3094+
locationset[y].markerid = marker.get('id');
3095+
30713096
if (
30723097
(_this.settings.fullMapStart === true && firstRun === true && _this.settings.querystringParams !== true) ||
30733098
(_this.settings.mapSettings.zoom === 0) ||

dist/assets/js/plugins/storeLocator/jquery.storelocator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ filtering.
3838
### Version 3.1.7
3939

4040
* Fixed openNearest setting not working in combination with querystringParams setting.
41+
* Fixed openNearest setting not opening correct location with custom sorting (sortBy) enabled.
4142

4243
### Version 3.1.6
4344

src/js/jquery.storelocator.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,15 +926,20 @@
926926
* Location distance sorting function
927927
*
928928
* @param locationsarray {array} locationset array
929+
* @param distanceOverride {boolean} Force sort by distance
929930
*/
930-
sortNumerically: function (locationsarray) {
931+
sortNumerically: function (locationsarray, distanceOverride) {
931932
this.writeDebug('sortNumerically',arguments);
932933
var property = (
933934
this.settings.sortBy !== null &&
934935
this.settings.sortBy.hasOwnProperty('prop') &&
935936
typeof this.settings.sortBy.prop !== 'undefined'
936937
) ? this.settings.sortBy.prop : 'distance';
937938

939+
if (typeof distanceOverride !== 'undefined' && distanceOverride === true) {
940+
property = 'distance';
941+
}
942+
938943
if (this.settings.sortBy !== null && this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
939944
locationsarray.sort(function (a, b) {
940945
return ((b[property] < a[property]) ? -1 : ((b[property] > a[property]) ? 1 : 0));
@@ -2466,7 +2471,7 @@
24662471
_this.settings.callbackNearestLoc.call(this, _this.map, nearestLoc, infowindow, storeStart, page);
24672472
}
24682473

2469-
var markerId = 0;
2474+
var markerId = (nearestLoc.hasOwnProperty('markerid')) ? nearestLoc.markerid : 0;
24702475
var selectedMarker = markers[markerId];
24712476

24722477
_this.createInfowindow(selectedMarker, 'left', infowindow, storeStart, page);
@@ -2860,6 +2865,23 @@
28602865

28612866
// Sorting
28622867
if (_this.settings.sortBy !== null && typeof _this.settings.sortBy === 'object') {
2868+
2869+
// Sort the multi-dimensional array by distance to get the nearest location first when enabled
2870+
if (_this.settings.openNearest === true) {
2871+
this.sortNumerically(locationset, true);
2872+
2873+
// Save the closest location to a variable for openNearest setting
2874+
if (typeof locationset[0] !== 'undefined') {
2875+
2876+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
2877+
nearestLoc = locationset[locationset.length - 1];
2878+
} else {
2879+
nearestLoc = locationset[0];
2880+
}
2881+
}
2882+
}
2883+
2884+
// Custom sorting
28632885
_this.sortCustom(locationset);
28642886
} else {
28652887
// Sort the multi-dimensional array by distance
@@ -2887,7 +2909,7 @@
28872909
}
28882910
}
28892911

2890-
// Save the closest location to a variable for openNearest setting.
2912+
// Save the closest location to a variable for openNearest setting
28912913
if (typeof locationset[0] !== 'undefined') {
28922914
nearestLoc = locationset[0];
28932915
}
@@ -3025,7 +3047,7 @@
30253047
_this.settings.callbackMapSet.call(this, _this.map, originPoint, originalZoom, myOptions);
30263048
}
30273049

3028-
// Initialize the infowondow
3050+
// Initialize the infowindow
30293051
if ( typeof InfoBubble !== 'undefined' && _this.settings.infoBubble !== null ) {
30303052
var infoBubbleSettings = _this.settings.infoBubble;
30313053
infoBubbleSettings.map = _this.map;
@@ -3035,7 +3057,6 @@
30353057
infowindow = new google.maps.InfoWindow();
30363058
}
30373059

3038-
30393060
// Add origin marker if the setting is set
30403061
_this.originMarker(_this.map, origin, originPoint);
30413062

@@ -3064,6 +3085,10 @@
30643085
marker = _this.createMarker(point, locationset[y].name, locationset[y].address, letter, _this.map, locationset[y].category);
30653086
marker.set('id', y);
30663087
markers[y] = marker;
3088+
3089+
// Add marker ID to location data
3090+
locationset[y].markerid = marker.get('id');
3091+
30673092
if (
30683093
(_this.settings.fullMapStart === true && firstRun === true && _this.settings.querystringParams !== true) ||
30693094
(_this.settings.mapSettings.zoom === 0) ||

0 commit comments

Comments
 (0)