Skip to content

Commit 28a0318

Browse files
committed
Update jquery.storelocator.js
Possibility to add custom variables in locations. If 'distance' variable is set in location, do not calculate it. Simplified some parts of the code.
1 parent 1ad204d commit 28a0318

File tree

1 file changed

+86
-140
lines changed

1 file changed

+86
-140
lines changed

js/jquery.storelocator.js

Lines changed: 86 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -377,38 +377,37 @@ $.fn.storeLocator = function(options) {
377377
if(settings.dataType === 'json' || settings.dataType === 'jsonp'){
378378
//Process JSON
379379
$.each(data, function(){
380-
var name = this.locname;
381-
var lat = this.lat;
382-
var lng = this.lng;
383-
var address = this.address;
384-
var address2 = this.address2;
385-
var city = this.city;
386-
var state = this.state;
387-
var postal = this.postal;
388-
var country = this.country;
389-
var phone = this.phone;
390-
var email = this.email;
391-
var web = this.web;
392-
if ( web ) web = web.replace("http://","");
393-
var hours1 = this.hours1;
394-
var hours2 = this.hours2;
395-
var hours3 = this.hours3;
396-
var category = this.category;
397-
var featured = this.featured;
398-
399-
var distance = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,lat,lng, GeoCodeCalc.EarthRadius);
400-
380+
var key, value, locationData = {};
381+
382+
// Parse each data variables
383+
for ( key in this ) {
384+
value = this[key];
385+
386+
if ( key == 'locname' ) {
387+
key = 'name'; // Translate locname to name (todo: should NOT be done)
388+
}
389+
else if ( key == 'web' ) {
390+
if ( value ) value = value.replace("http://",""); // Remove scheme (todo: should NOT be done)
391+
}
392+
393+
locationData[key] = value;
394+
}
395+
396+
if ( !locationData['distance'] ) {
397+
locationData['distance'] = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,locationData['lat'],locationData['lng'], GeoCodeCalc.EarthRadius);
398+
}
399+
401400
//Create the array
402401
if(settings.maxDistance === true && firstRun !== true){
403-
if(distance < maxDistance){
404-
locationset[i] = [distance, name, lat, lng, address, address2, city, state, postal, country, phone, email, web, hours1, hours2, hours3, category, featured];
402+
if(locationData['distance'] < maxDistance){
403+
locationset[i] = locationData;
405404
}
406405
else{
407406
return;
408407
}
409408
}
410409
else{
411-
locationset[i] = [distance, name, lat, lng, address, address2, city, state, postal, country, phone, email, web, hours1, hours2, hours3, category, featured];
410+
locationset[i] = locationData;
412411
}
413412

414413
i++;
@@ -417,24 +416,26 @@ $.fn.storeLocator = function(options) {
417416
else if(settings.dataType === 'kml'){
418417
//Process KML
419418
$(data).find('Placemark').each(function(){
420-
var name = $(this).find('name').text();
421-
var lat = $(this).find('coordinates').text().split(",")[1];
422-
var lng = $(this).find('coordinates').text().split(",")[0];
423-
var description = $(this).find('description').text();
419+
var locationData = {
420+
'name': $(this).find('name').text(),
421+
'lat': $(this).find('coordinates').text().split(",")[1],
422+
'lng': $(this).find('coordinates').text().split(",")[0],
423+
'description': $(this).find('description').text()
424+
};
424425

425-
var distance = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,lat,lng, GeoCodeCalc.EarthRadius);
426+
locationData['distance'] = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,locationData['lat'],locationData['lng'], GeoCodeCalc.EarthRadius);
426427

427428
//Create the array
428429
if(settings.maxDistance === true && firstRun !== true){
429-
if(distance < maxDistance){
430-
locationset[i] = [distance, name, lat, lng, description];
430+
if(locationData['distance'] < maxDistance){
431+
locationset[i] = locationData;
431432
}
432433
else{
433434
return;
434435
}
435436
}
436437
else{
437-
locationset[i] = [distance, name, lat, lng, description];
438+
locationset[i] = locationData;
438439
}
439440

440441
i++;
@@ -443,38 +444,41 @@ $.fn.storeLocator = function(options) {
443444
else{
444445
//Process XML
445446
$(data).find('marker').each(function(){
446-
var name = $(this).attr('name');
447-
var lat = $(this).attr('lat');
448-
var lng = $(this).attr('lng');
449-
var address = $(this).attr('address');
450-
var address2 = $(this).attr('address2');
451-
var city = $(this).attr('city');
452-
var state = $(this).attr('state');
453-
var postal = $(this).attr('postal');
454-
var country = $(this).attr('country');
455-
var phone = $(this).attr('phone');
456-
var email = $(this).attr('email');
457-
var web = $(this).attr('web');
458-
if ( web ) web = web.replace("http://","");
459-
var hours1 = $(this).attr('hours1');
460-
var hours2 = $(this).attr('hours2');
461-
var hours3 = $(this).attr('hours3');
462-
var category = $(this).attr('category');
463-
var featured = $(this).attr('featured');
464-
465-
var distance = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,lat,lng, GeoCodeCalc.EarthRadius);
447+
var locationData = {
448+
'name': $(this).attr('name'),
449+
'lat': $(this).attr('lat'),
450+
'lng': $(this).attr('lng'),
451+
'address': $(this).attr('address'),
452+
'address2': $(this).attr('address2'),
453+
'city': $(this).attr('city'),
454+
'state': $(this).attr('state'),
455+
'postal': $(this).attr('postal'),
456+
'country': $(this).attr('country'),
457+
'phone': $(this).attr('phone'),
458+
'email': $(this).attr('email'),
459+
'web': $(this).attr('web'),
460+
'hours1': $(this).attr('hours1'),
461+
'hours2': $(this).attr('hours2'),
462+
'hours3': $(this).attr('hours3'),
463+
'category': $(this).attr('category'),
464+
'featured': $(this).attr('featured')
465+
};
466+
467+
if ( locationData['web'] ) locationData['web'] = locationData['web'].replace("http://",""); // Remove scheme (todo: should NOT be done)
468+
469+
locationData['distance'] = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,locationData['lat'],locationData['lng'], GeoCodeCalc.EarthRadius);
466470

467471
//Create the array
468472
if(settings.maxDistance === true && firstRun !== true){
469-
if(distance < maxDistance){
470-
locationset[i] = [distance, name, lat, lng, address, address2, city, state, postal, country, phone, email, web, hours1, hours2, hours3, category, featured];
473+
if(locationData['distance'] < maxDistance){
474+
locationset[i] = locationData;
471475
}
472476
else{
473477
return;
474478
}
475479
}
476480
else{
477-
locationset[i] = [distance, name, lat, lng, address, address2, city, state, postal, country, phone, email, web, hours1, hours2, hours3, category, featured];
481+
locationset[i] = locationData;
478482
}
479483

480484
i++;
@@ -484,9 +488,7 @@ $.fn.storeLocator = function(options) {
484488
//Distance sorting function
485489
function sort_numerically(locationsarray){
486490
locationsarray.sort(function(a, b){
487-
var x = a[0];
488-
var y = b[0];
489-
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
491+
return ((a['distance'] < b['distance']) ? -1 : ((a['distance'] > b['distance']) ? 1 : 0));
490492
});
491493
}
492494

@@ -497,12 +499,12 @@ $.fn.storeLocator = function(options) {
497499
if(settings.featuredLocations === true){
498500
//Create array for featured locations
499501
featuredset = $.grep(locationset, function(val, i){
500-
return val[17] === "true";
502+
return val['featured'] === "true";
501503
});
502504

503505
//Create array for normal locations
504506
normalset = $.grep(locationset, function(val, i){
505-
return val[17] !== "true";
507+
return val['featured'] !== "true";
506508
});
507509

508510
//Combine the arrays
@@ -515,62 +517,41 @@ $.fn.storeLocator = function(options) {
515517

516518
//Check the closest marker
517519
if(settings.maxDistance === true && firstRun !== true){
518-
if(locationset[0] === undefined || locationset[0][0] > maxDistance){
520+
if(locationset[0] === undefined || locationset[0]['distance'] > maxDistance){
519521
alert(settings.distanceErrorAlert + maxDistance + " " + distUnit);
520522
return;
521523
}
522524
}
523525
else{
524-
if(settings.distanceAlert != -1 && locationset[0][0] > settings.distanceAlert){
526+
if(settings.distanceAlert != -1 && locationset[0]['distance'] > settings.distanceAlert){
525527
alert(settings.distanceErrorAlert + settings.distanceAlert + " " + distUnit);
526528
}
527529
}
528530

529531
//Create the map with jQuery
530532
$(function(){
531533

532-
var storeDistance, storeName, storeAddress1, storeAddress2, storeCity, storeState, storeCountry, storeZip, storePhone, storeEmail, storeWeb, storeHours1, storeHours2, storeHours3, storeDescription, storeCat, storeFeatured;
534+
var key, value, locationData = {};
533535

534536
//Instead of repeating the same thing twice below
535537
function create_location_variables(loopcount){
536-
storeDistance = locationset[loopcount][0];
537-
storeDistance = roundNumber(storeDistance,2);
538-
storeName = locationset[loopcount][1];
539-
storeAddress1 = locationset[loopcount][4];
540-
storeAddress2 = locationset[loopcount][5];
541-
storeCity = locationset[loopcount][6];
542-
storeState = locationset[loopcount][7];
543-
storeZip = locationset[loopcount][8];
544-
storeCountry = locationset[loopcount][9];
545-
storePhone = locationset[loopcount][10];
546-
storeEmail = locationset[loopcount][11]
547-
storeWeb = locationset[loopcount][12];
548-
storeHours1 = locationset[loopcount][13];
549-
storeHours2 = locationset[loopcount][14];
550-
storeHours3 = locationset[loopcount][15];
551-
storeCat = locationset[loopcount][16];
552-
storeFeatured = locationset[loopcount][17];
553-
}
538+
for ( key in locationset[loopcount] ) {
539+
value = locationset[loopcount][key];
554540

555-
//There are less variables for KML files
556-
function create_kml_location_variables(loopcount){
557-
storeDistance = locationset[loopcount][0];
558-
storeDistance = roundNumber(storeDistance,2);
559-
storeName = locationset[loopcount][1];
560-
storeDescription = locationset[loopcount][4];
541+
if ( key == 'distance' ) {
542+
value = roundNumber(value,2);
543+
}
544+
545+
locationData[key] = value;
546+
}
561547
}
562548

563549
//Define the location data for the templates
564550
function define_location_data(currentMarker){
565-
if(settings.dataType === 'kml'){
566-
create_kml_location_variables(currentMarker.get("id"));
567-
}
568-
else{
569-
create_location_variables(currentMarker.get("id"));
570-
}
551+
create_location_variables(currentMarker.get("id"));
571552

572553
var distLength;
573-
if(storeDistance <= 1){
554+
if(locationData['distance'] <= 1){
574555
if(settings.lengthUnit === "km"){
575556
distLength = settings.kilometerLang;
576557
}
@@ -598,49 +579,14 @@ $.fn.storeLocator = function(options) {
598579
}
599580

600581
//Define location data
601-
if(settings.dataType === 'kml'){
602-
var locations = {
603-
location: [
604-
{
605-
"distance": storeDistance,
606-
"markerid": markerId,
607-
"marker": indicator,
608-
"name": storeName,
609-
"description": storeDescription,
610-
"length": distLength,
611-
"origin": origin
612-
}
613-
]
614-
};
615-
}
616-
else{
617-
var locations = {
618-
location: [
619-
{
620-
"distance": storeDistance,
621-
"markerid": markerId,
622-
"marker": indicator,
623-
"name": storeName,
624-
"address": storeAddress1,
625-
"address2": storeAddress2,
626-
"city": storeCity,
627-
"state": storeState,
628-
"postal": storeZip,
629-
"country": storeCountry,
630-
"phone": storePhone,
631-
"email": storeEmail,
632-
"web": storeWeb,
633-
"hours1": storeHours1,
634-
"hours2": storeHours2,
635-
"hours3": storeHours3,
636-
"length": distLength,
637-
"origin": origin,
638-
"category": storeCat,
639-
"featured": storeFeatured
640-
}
641-
]
642-
};
643-
}
582+
var locations = {
583+
location: [$.extend(locationData, {
584+
'markerid': markerId,
585+
'marker': indicator,
586+
'length': distLength,
587+
'origin': origin
588+
})]
589+
};
644590

645591
return locations;
646592
}
@@ -729,8 +675,8 @@ $.fn.storeLocator = function(options) {
729675
//Add markers and infowindows loop
730676
for(var y = 0; y <= storenum; y++) {
731677
var letter = String.fromCharCode("A".charCodeAt(0) + y);
732-
var point = new google.maps.LatLng(locationset[y][2], locationset[y][3]);
733-
marker = createMarker(point, locationset[y][1], locationset[y][4], letter, locationset[y][14]);
678+
var point = new google.maps.LatLng(locationset[y]['lat'], locationset[y]['lng']);
679+
marker = createMarker(point, locationset[y]['name'], locationset[y]['address'], letter);
734680
marker.set("id", y);
735681
markers[y] = marker;
736682
if((settings.fullMapStart === true && firstRun === true) || settings.zoomLevel === 0){
@@ -789,7 +735,7 @@ $.fn.storeLocator = function(options) {
789735
$("#" + settings.listDiv + " ul li:odd").css('background', "#" + settings.listColor2);
790736

791737
//Custom marker function - alphabetical
792-
function createMarker(point, name, address, letter, type){
738+
function createMarker(point, name, address, letter){
793739
//Set up pin icon with the Google Charts API for all of our markers
794740
var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + letter + "|" + settings.pinColor + "|" + settings.pinTextColor,
795741
new google.maps.Size(21, 34),

0 commit comments

Comments
 (0)