Skip to content

Commit f9ba23b

Browse files
committed
Added openNearest setting to open/select the nearest location after searching, added corresponding callbackNearestLoc
1 parent 1094cb2 commit f9ba23b

File tree

7 files changed

+131
-12
lines changed

7 files changed

+131
-12
lines changed

callbacks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ $('#bh-sl-map-container').storeLocator({
4444
| [callbackModalOpen](callbacks/callback-modalopen.md) | Modal open callback |
4545
| [callbackModalReady](callbacks/callback-modalready.md) | Modal ready callback |
4646
| [callbackModalClose](callbacks/callback-modalclose.md) | Modal close callback |
47+
| [callbackNearestLoc](callbacks/callback-nearestloc.md) | Nearest location callback |
4748
| [callbackMarkerClick](callbacks/callback-markerclick.md) | Marker click callback |
4849
| [callbackListClick](callbacks/callback-listclick.md) | Location list click callback |
4950
| [callbackPageChange](callbacks/callback-pagechange.md) | Page change callback |

callbacks/callback-nearestloc.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# callbackNearestLoc
2+
3+
## Description
4+
5+
Fires when the nearest location is triggered with the openNearest setting.
6+
7+
## Parameters
8+
9+
| Name | Type | Description |
10+
|---|---|---|
11+
| map | object | Google Maps object |
12+
| nearestLoc | object | Details of the nearest location |
13+
| infowindow | object | Info window object |
14+
| storeStart | number | Starting point of current page when pagination is enabled |
15+
| page | number | Current page number when pagination is enabled |

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery Google Maps Store Locator - v2.7.5 - 2018-02-08
1+
/*! jQuery Google Maps Store Locator - v2.7.5 - 2018-02-09
22
* http://www.bjornblog.com/web/jquery-store-locator-plugin
33
* Copyright (c) 2018 Bjorn Holine; Licensed MIT */
44

@@ -79,6 +79,7 @@
7979
'nameAttribute' : 'name',
8080
'visibleMarkersList' : false,
8181
'dragSearch' : false,
82+
'openNearest' : false,
8283
'infowindowTemplatePath' : 'assets/js/plugins/storeLocator/templates/infowindow-description.html',
8384
'listTemplatePath' : 'assets/js/plugins/storeLocator/templates/location-list-description.html',
8485
'KMLinfowindowTemplatePath' : 'assets/js/plugins/storeLocator/templates/kml-infowindow-description.html',
@@ -104,6 +105,7 @@
104105
'callbackModalOpen' : null,
105106
'callbackModalReady' : null,
106107
'callbackModalClose' : null,
108+
'callbackNearestLoc' : null,
107109
'callbackJsonp' : null,
108110
'callbackCreateMarker' : null,
109111
'callbackPageChange' : null,
@@ -2162,13 +2164,53 @@
21622164
});
21632165
},
21642166

2167+
/**
2168+
* Open and select the location closest to the origin
2169+
*
2170+
* @param nearestLoc {Object} Details for the nearest location
2171+
* @param infowindow {Object} Info window object
2172+
* @param storeStart {number} Starting point of current page when pagination is enabled
2173+
* @param page {number} Current page number when pagination is enabled
2174+
*/
2175+
openNearestLocation: function(nearestLoc, infowindow, storeStart, page) {
2176+
this.writeDebug('openNearestLocation',arguments);
2177+
2178+
if (this.settings.openNearest !== true || typeof nearestLoc === 'undefined' || (this.settings.fullMapStart === true && firstRun === true) || (this.settings.defaultLoc === true && firstRun === true)) {
2179+
return;
2180+
}
2181+
2182+
var _this = this;
2183+
2184+
// Callback
2185+
if (_this.settings.callbackNearestLoc) {
2186+
_this.settings.callbackNearestLoc.call(this, _this.map, nearestLoc, infowindow, storeStart, page);
2187+
}
2188+
2189+
var markerId = 0;
2190+
var selectedMarker = markers[markerId];
2191+
2192+
_this.createInfowindow(selectedMarker, 'left', infowindow, storeStart, page);
2193+
2194+
// Scroll list to selected marker
2195+
var $container = $('.' + _this.settings.locationList);
2196+
var $selectedLocation = $('.' + _this.settings.locationList + ' li[data-markerid=' + markerId + ']');
2197+
2198+
// Focus on the list
2199+
$('.' + _this.settings.locationList + ' li').removeClass('list-focus');
2200+
$selectedLocation.addClass('list-focus');
2201+
2202+
$container.animate({
2203+
scrollTop: $selectedLocation.offset().top - $container.offset().top + $container.scrollTop()
2204+
});
2205+
},
2206+
21652207
/**
21662208
* Handle clicks from the location list
21672209
*
21682210
* @param map {Object} Google map
2169-
* @param infowindow
2170-
* @param storeStart
2171-
* @param page
2211+
* @param infowindow {Object} Info window object
2212+
* @param storeStart {number} Starting point of current page when pagination is enabled
2213+
* @param page {number} Current page number when pagination is enabled
21722214
*/
21732215
listClick: function(map, infowindow, storeStart, page) {
21742216
this.writeDebug('listClick',arguments);
@@ -2363,7 +2405,7 @@
23632405
this.writeDebug('processData',arguments);
23642406
var _this = this;
23652407
var i = 0;
2366-
var orig_lat, orig_lng, origin, name, maxDistance, marker, bounds, storeStart, storeNumToShow, myOptions, distError, openMap, infowindow;
2408+
var orig_lat, orig_lng, origin, name, maxDistance, marker, bounds, storeStart, storeNumToShow, myOptions, distError, openMap, infowindow, nearestLoc;
23672409
var taxFilters = {};
23682410
if (!this.isEmptyObject(mappingObject)) {
23692411
orig_lat = mappingObject.lat;
@@ -2511,6 +2553,11 @@
25112553
}
25122554
}
25132555

2556+
// Save the closest location to a variable for openNearest setting.
2557+
if (_this.settings.openNearest === true && typeof locationset[0] !== 'undefined') {
2558+
nearestLoc = locationset[0];
2559+
}
2560+
25142561
// Featured locations filtering
25152562
if (_this.settings.featuredLocations === true) {
25162563
// Create array for featured locations
@@ -2692,6 +2739,9 @@
26922739
});
26932740
}
26942741

2742+
// Open nearest location.
2743+
_this.openNearestLocation(nearestLoc, infowindow, storeStart, page);
2744+
26952745
// MarkerClusterer setup
26962746
if ( typeof MarkerClusterer !== 'undefined' && _this.settings.markerCluster !== null ) {
26972747
var markerCluster = new MarkerClusterer(_this.map, markers, _this.settings.markerCluster);

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

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
| nameAttribute | 'name' | If using nameSearch, the data attribute used for the location name in the data file. |
5858
| visibleMarkersList | false | Set to true to have the location list only show data from markers that are visible on the map. |
5959
| dragSearch | false | Set to true to perform a new search after the map is dragged. |
60+
| openNearest | false | Set to true to highlight the nearest location automatically after searching. |
6061
| infowindowTemplatePath | 'assets/js/plugins/storeLocator/templates/infowindow-description.html' | Path to the default infowindow template. |
6162
| listTemplatePath | 'assets/js/plugins/storeLocator/templates/location-list-description.html' | Path to the default list template. |
6263
| KMLinfowindowTemplatePath | 'assets/js/plugins/storeLocator/templates/kml-infowindow-description.html' | Path to the KML infowindow template – used if dataType is set to kml. |

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ filtering.
3939

4040
* Added callbackAutoGeoSuccess callback that fires after the geolocation API returns a successful result.
4141
* Added callbackFormVals callback that fires after the form values have been processed from the form.
42+
* Added callbackNearestLoc that fires when the nearest location is triggered with the openNearest setting.
43+
* Added openNearest setting to open/select the nearest location after searching.
4244
* Fixed issue with featuredLocations setting where featured locations at far distances would trigger distance alert.
4345
* Fixed issue with filtering values containing ampersands, which would not display any results - updated filtering regex.
4446
* Fixed issue where HTML5 Geolocation was skipped when using the fullMapStartBlank setting.

src/js/jquery.storelocator.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
'nameAttribute' : 'name',
7676
'visibleMarkersList' : false,
7777
'dragSearch' : false,
78+
'openNearest' : false,
7879
'infowindowTemplatePath' : 'assets/js/plugins/storeLocator/templates/infowindow-description.html',
7980
'listTemplatePath' : 'assets/js/plugins/storeLocator/templates/location-list-description.html',
8081
'KMLinfowindowTemplatePath' : 'assets/js/plugins/storeLocator/templates/kml-infowindow-description.html',
@@ -100,6 +101,7 @@
100101
'callbackModalOpen' : null,
101102
'callbackModalReady' : null,
102103
'callbackModalClose' : null,
104+
'callbackNearestLoc' : null,
103105
'callbackJsonp' : null,
104106
'callbackCreateMarker' : null,
105107
'callbackPageChange' : null,
@@ -2158,13 +2160,53 @@
21582160
});
21592161
},
21602162

2163+
/**
2164+
* Open and select the location closest to the origin
2165+
*
2166+
* @param nearestLoc {Object} Details for the nearest location
2167+
* @param infowindow {Object} Info window object
2168+
* @param storeStart {number} Starting point of current page when pagination is enabled
2169+
* @param page {number} Current page number when pagination is enabled
2170+
*/
2171+
openNearestLocation: function(nearestLoc, infowindow, storeStart, page) {
2172+
this.writeDebug('openNearestLocation',arguments);
2173+
2174+
if (this.settings.openNearest !== true || typeof nearestLoc === 'undefined' || (this.settings.fullMapStart === true && firstRun === true) || (this.settings.defaultLoc === true && firstRun === true)) {
2175+
return;
2176+
}
2177+
2178+
var _this = this;
2179+
2180+
// Callback
2181+
if (_this.settings.callbackNearestLoc) {
2182+
_this.settings.callbackNearestLoc.call(this, _this.map, nearestLoc, infowindow, storeStart, page);
2183+
}
2184+
2185+
var markerId = 0;
2186+
var selectedMarker = markers[markerId];
2187+
2188+
_this.createInfowindow(selectedMarker, 'left', infowindow, storeStart, page);
2189+
2190+
// Scroll list to selected marker
2191+
var $container = $('.' + _this.settings.locationList);
2192+
var $selectedLocation = $('.' + _this.settings.locationList + ' li[data-markerid=' + markerId + ']');
2193+
2194+
// Focus on the list
2195+
$('.' + _this.settings.locationList + ' li').removeClass('list-focus');
2196+
$selectedLocation.addClass('list-focus');
2197+
2198+
$container.animate({
2199+
scrollTop: $selectedLocation.offset().top - $container.offset().top + $container.scrollTop()
2200+
});
2201+
},
2202+
21612203
/**
21622204
* Handle clicks from the location list
21632205
*
21642206
* @param map {Object} Google map
2165-
* @param infowindow
2166-
* @param storeStart
2167-
* @param page
2207+
* @param infowindow {Object} Info window object
2208+
* @param storeStart {number} Starting point of current page when pagination is enabled
2209+
* @param page {number} Current page number when pagination is enabled
21682210
*/
21692211
listClick: function(map, infowindow, storeStart, page) {
21702212
this.writeDebug('listClick',arguments);
@@ -2359,7 +2401,7 @@
23592401
this.writeDebug('processData',arguments);
23602402
var _this = this;
23612403
var i = 0;
2362-
var orig_lat, orig_lng, origin, name, maxDistance, marker, bounds, storeStart, storeNumToShow, myOptions, distError, openMap, infowindow;
2404+
var orig_lat, orig_lng, origin, name, maxDistance, marker, bounds, storeStart, storeNumToShow, myOptions, distError, openMap, infowindow, nearestLoc;
23632405
var taxFilters = {};
23642406
if (!this.isEmptyObject(mappingObject)) {
23652407
orig_lat = mappingObject.lat;
@@ -2507,6 +2549,11 @@
25072549
}
25082550
}
25092551

2552+
// Save the closest location to a variable for openNearest setting.
2553+
if (_this.settings.openNearest === true && typeof locationset[0] !== 'undefined') {
2554+
nearestLoc = locationset[0];
2555+
}
2556+
25102557
// Featured locations filtering
25112558
if (_this.settings.featuredLocations === true) {
25122559
// Create array for featured locations
@@ -2688,6 +2735,9 @@
26882735
});
26892736
}
26902737

2738+
// Open nearest location.
2739+
_this.openNearestLocation(nearestLoc, infowindow, storeStart, page);
2740+
26912741
// MarkerClusterer setup
26922742
if ( typeof MarkerClusterer !== 'undefined' && _this.settings.markerCluster !== null ) {
26932743
var markerCluster = new MarkerClusterer(_this.map, markers, _this.settings.markerCluster);

0 commit comments

Comments
 (0)