Skip to content

Commit 36c44d7

Browse files
committed
Optimized location data loops
1 parent eb904d9 commit 36c44d7

File tree

4 files changed

+97
-130
lines changed

4 files changed

+97
-130
lines changed

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

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery Google Maps Store Locator - v2.0.0 - 2014-10-21
1+
/*! jQuery Google Maps Store Locator - v2.0.0 - 2014-10-26
22
* http://www.bjornblog.com/web/jquery-store-locator-plugin
33
* Copyright (c) 2014 Bjorn Holine; Licensed MIT */
44

@@ -1180,6 +1180,38 @@
11801180
}
11811181
},
11821182

1183+
/**
1184+
* Checks distance of each location and setups up the locationset array
1185+
*
1186+
* @param data {Object} location data object
1187+
* @param l {number} iterator from the loop processing the data in the mapping function below
1188+
* @param lat {number} origin latitude
1189+
* @param lng {number} origin longitude
1190+
* @param firstRun {boolean} inital load check
1191+
* @param origin {string} origin address
1192+
* @param maxDistance {number} maximum distance if set
1193+
*/
1194+
locationsSetup: function (data, l, lat, lng, firstRun, origin, maxDistance) {
1195+
if (typeof origin !== 'undefined') {
1196+
if (!data.distance) {
1197+
data.distance = this.geoCodeCalcCalcDistance(lat, lng, data.lat, data.lng, GeoCodeCalc.EarthRadius);
1198+
}
1199+
}
1200+
1201+
// Create the array
1202+
if (this.settings.maxDistance === true && firstRun !== true && maxDistance !== null) {
1203+
if (data.distance < maxDistance) {
1204+
locationset[l] = data;
1205+
}
1206+
else {
1207+
return;
1208+
}
1209+
}
1210+
else {
1211+
locationset[l] = data;
1212+
}
1213+
},
1214+
11831215
/**
11841216
* The primary mapping function that runs everything
11851217
*
@@ -1254,40 +1286,23 @@
12541286

12551287
// Process the location data depending on the data format type
12561288
if (_this.settings.dataType === 'json' || _this.settings.dataType === 'jsonp') {
1289+
12571290
// Process JSON
1258-
$.each(data, function () {
1259-
var key, value, locationData = {};
1291+
for(var x = 0; i < data.length; x++){
1292+
var obj = data[x];
1293+
var locationData = {};
12601294

12611295
// Parse each data variable
1262-
for (key in this) {
1263-
if (this.hasOwnProperty(key)) {
1264-
value = this[key];
1265-
1266-
locationData[key] = value;
1296+
for (var key in obj) {
1297+
if (obj.hasOwnProperty(key)) {
1298+
locationData[key] = obj[key];
12671299
}
12681300
}
12691301

1270-
if (typeof origin !== 'undefined') {
1271-
if (!locationData.distance) {
1272-
locationData.distance = _this.geoCodeCalcCalcDistance(orig_lat, orig_lng, locationData.lat, locationData.lng, GeoCodeCalc.EarthRadius);
1273-
}
1274-
}
1275-
1276-
// Create the array
1277-
if (_this.settings.maxDistance === true && firstRun !== true && maxDistance !== null) {
1278-
if (locationData.distance < maxDistance) {
1279-
locationset[i] = locationData;
1280-
}
1281-
else {
1282-
return;
1283-
}
1284-
}
1285-
else {
1286-
locationset[i] = locationData;
1287-
}
1302+
_this.locationsSetup(locationData, i, orig_lat, orig_lng, firstRun, origin, maxDistance);
12881303

12891304
i++;
1290-
});
1305+
}
12911306
}
12921307
else if (_this.settings.dataType === 'kml') {
12931308
// Process KML
@@ -1299,24 +1314,7 @@
12991314
'description': $(this).find('description').text()
13001315
};
13011316

1302-
if (typeof origin !== 'undefined') {
1303-
if (!locationData.distance) {
1304-
locationData.distance = _this.geoCodeCalcCalcDistance(orig_lat, orig_lng, locationData.lat, locationData.lng, GeoCodeCalc.EarthRadius);
1305-
}
1306-
}
1307-
1308-
// Create the array
1309-
if (_this.settings.maxDistance === true && firstRun !== true && maxDistance) {
1310-
if (locationData.distance < maxDistance) {
1311-
locationset[i] = locationData;
1312-
}
1313-
else {
1314-
return;
1315-
}
1316-
}
1317-
else {
1318-
locationset[i] = locationData;
1319-
}
1317+
_this.locationsSetup(locationData, i, orig_lat, orig_lng, firstRun, origin, maxDistance);
13201318

13211319
i++;
13221320
});
@@ -1325,29 +1323,14 @@
13251323
// Process XML
13261324
$(data).find(_this.settings.xmlElement).each(function () {
13271325
var locationData = {};
1328-
1329-
$.each(this.attributes, function (i, attrib) {
1330-
locationData[attrib.name] = attrib.value;
1331-
});
13321326

1333-
if (typeof origin !== 'undefined') {
1334-
if (!locationData.distance) {
1335-
locationData.distance = _this.geoCodeCalcCalcDistance(orig_lat, orig_lng, locationData.lat, locationData.lng, GeoCodeCalc.EarthRadius);
1327+
for (var key in this.attributes) {
1328+
if (this.attributes.hasOwnProperty(key)) {
1329+
locationData[this.attributes[key].name] = this.attributes[key].value;
13361330
}
13371331
}
13381332

1339-
// Create the array
1340-
if (_this.settings.maxDistance === true && firstRun !== true && maxDistance) {
1341-
if (locationData.distance < maxDistance) {
1342-
locationset[i] = locationData;
1343-
}
1344-
else {
1345-
return;
1346-
}
1347-
}
1348-
else {
1349-
locationset[i] = locationData;
1350-
}
1333+
_this.locationsSetup(locationData, i, orig_lat, orig_lng, firstRun, origin, maxDistance);
13511334

13521335
i++;
13531336
});

0 commit comments

Comments
 (0)