1
1
/*
2
- * storeLocator v1.1.3 - jQuery store locator plugin
2
+ * storeLocator v1.2 - jQuery store locator plugin
3
3
* (c) Copyright 2012, Bjorn Holine (http://www.bjornblog.com)
4
4
* Released under the MIT license
5
5
* Distance calculation function by Chris Pietschmann: http://pietschsoft.com/post/2008/02/01/Calculate-Distance-Between-Geocodes-in-C-and-JavaScript.aspx
@@ -12,17 +12,22 @@ $.fn.storeLocator = function(options) {
12
12
'mapDiv' : 'map' ,
13
13
'listDiv' : 'list' ,
14
14
'formID' : 'user-location' ,
15
+ 'inputID' : 'address' ,
15
16
'zoomLevel' : 12 ,
16
17
'pinColor' : 'fe7569' ,
17
18
'pinTextColor' : '000000' ,
18
19
'storeLimit' : 26 ,
19
20
'distanceAlert' : 60 ,
20
- 'xmlLocation' : 'locations.xml' ,
21
+ 'dataLocation' : 'locations.xml' ,
21
22
'listColor1' : 'ffffff' ,
22
23
'listColor2' : 'eeeeee' ,
23
24
'bounceMarker' : true ,
24
25
'slideMap' : true ,
25
- 'modalWindow' : false
26
+ 'modalWindow' : false ,
27
+ 'defaultLoc' : false ,
28
+ 'defaultLat' : '' ,
29
+ 'defaultLng' : '' ,
30
+ 'jsonData' : false
26
31
} , options ) ;
27
32
28
33
return this . each ( function ( ) {
@@ -43,6 +48,11 @@ $.fn.storeLocator = function(options) {
43
48
$this . hide ( ) ;
44
49
}
45
50
51
+ if ( settings . defaultLoc == true )
52
+ {
53
+ mapping ( settings . defaultLat , settings . defaultLng ) ;
54
+ }
55
+
46
56
var userinput , olat , olng , marker , letter , geocoder , storenum ;
47
57
var locationset = new Array ( ) ;
48
58
@@ -77,13 +87,20 @@ $.fn.storeLocator = function(options) {
77
87
} ;
78
88
}
79
89
90
+ //Used to round miles to display
91
+ function roundNumber ( num , dec )
92
+ {
93
+ var result = Math . round ( num * Math . pow ( 10 , dec ) ) / Math . pow ( 10 , dec ) ;
94
+ return result ;
95
+ }
96
+
80
97
//Process form input
81
98
$ ( function ( ) {
82
99
$ ( document ) . on ( 'submit' , '#' + settings . formID , function ( e ) {
83
100
//Stop the form submission
84
101
e . preventDefault ( ) ;
85
102
//Get the user input and use it
86
- var userinput = $ ( '#' + settings . formID ) . serialize ( ) ;
103
+ var userinput = $ ( '#' + settings . formID + ' #' + settings . inputID ) . serialize ( ) ;
87
104
userinput = userinput . replace ( "address=" , "" ) ;
88
105
if ( userinput == "" )
89
106
{
@@ -104,9 +121,6 @@ $.fn.storeLocator = function(options) {
104
121
alert ( 'ERROR! Unable to geocode address' ) ;
105
122
}
106
123
} ) ;
107
-
108
- //Replace spaces in user input
109
- userinput = userinput . replace ( " " , "+" ) ;
110
124
}
111
125
112
126
} ) ;
@@ -116,39 +130,79 @@ $.fn.storeLocator = function(options) {
116
130
//Now all the mapping stuff
117
131
function mapping ( orig_lat , orig_lng ) {
118
132
$ ( function ( ) {
119
- //Parse xml with jQuery
133
+
134
+ var dataType ;
135
+
136
+ //JSON or XML?
137
+ if ( settings . jsonData == true ) { dataType = "json" ; }
138
+ else { dataType = "xml" ; }
139
+
120
140
$ . ajax ( {
121
141
type : "GET" ,
122
- url : settings . xmlLocation ,
123
- dataType : "xml" ,
124
- success : function ( xml ) {
125
-
126
- //After the store locations file has been read successfully
127
- var i = 0 ;
128
-
129
- $ ( xml ) . find ( 'marker' ) . each ( function ( ) {
130
- //Take the lat lng from the user, geocoded above
131
- var name = $ ( this ) . attr ( 'name' ) ;
132
- var lat = $ ( this ) . attr ( 'lat' ) ;
133
- var lng = $ ( this ) . attr ( 'lng' ) ;
134
- var address = $ ( this ) . attr ( 'address' ) ;
135
- var address2 = $ ( this ) . attr ( 'address2' ) ;
136
- var city = $ ( this ) . attr ( 'city' ) ;
137
- var state = $ ( this ) . attr ( 'state' ) ;
138
- var postal = $ ( this ) . attr ( 'postal' ) ;
139
- var phone = $ ( this ) . attr ( 'phone' ) ;
140
- var web = $ ( this ) . attr ( 'web' ) ;
141
- web = web . replace ( "http://" , "" ) ;
142
- var hours1 = $ ( this ) . attr ( 'hours1' ) ;
143
- var hours2 = $ ( this ) . attr ( 'hours2' ) ;
144
- var hours3 = $ ( this ) . attr ( 'hours3' ) ;
145
- var distance = GeoCodeCalc . CalcDistance ( orig_lat , orig_lng , lat , lng , GeoCodeCalc . EarthRadiusInMiles ) ;
146
-
147
- //Create the array
148
- locationset [ i ] = new Array ( distance , name , lat , lng , address , address2 , city , state , postal , phone , web , hours1 , hours2 , hours3 ) ;
149
-
150
- i ++ ;
151
- } ) ;
142
+ url : settings . dataLocation ,
143
+ dataType : dataType ,
144
+ success : function ( data ) {
145
+
146
+ //After the store locations file has been read successfully
147
+ var i = 0 ;
148
+
149
+ //Depending on your data strucutre and what you want to include in the maps, you may need to change the following variables or comment them out
150
+ if ( settings . jsonData == true )
151
+ {
152
+ //Process JSON
153
+ $ . each ( data , function ( ) {
154
+
155
+ var name = this . locname ;
156
+ var lat = this . lat ;
157
+ var lng = this . lng ;
158
+ var address = this . address ;
159
+ var address2 = this . address2 ;
160
+ var city = this . city ;
161
+ var state = this . state ;
162
+ var postal = this . postal ;
163
+ var phone = this . phone ;
164
+ var web = this . web ;
165
+ web = web . replace ( "http://" , "" ) ;
166
+ var hours1 = this . hours1 ;
167
+ var hours2 = this . hours2 ;
168
+ var hours3 = this . hours3 ;
169
+
170
+ var distance = GeoCodeCalc . CalcDistance ( orig_lat , orig_lng , lat , lng , GeoCodeCalc . EarthRadiusInMiles ) ;
171
+
172
+ //Create the array
173
+ locationset [ i ] = new Array ( distance , name , lat , lng , address , address2 , city , state , postal , phone , web , hours1 , hours2 , hours3 ) ;
174
+
175
+ i ++ ;
176
+ } ) ;
177
+ }
178
+ else
179
+ {
180
+ //Process XML
181
+ $ ( data ) . find ( 'marker' ) . each ( function ( ) {
182
+ //Take the lat lng from the user, geocoded above
183
+ var name = $ ( this ) . attr ( 'name' ) ;
184
+ var lat = $ ( this ) . attr ( 'lat' ) ;
185
+ var lng = $ ( this ) . attr ( 'lng' ) ;
186
+ var address = $ ( this ) . attr ( 'address' ) ;
187
+ var address2 = $ ( this ) . attr ( 'address2' ) ;
188
+ var city = $ ( this ) . attr ( 'city' ) ;
189
+ var state = $ ( this ) . attr ( 'state' ) ;
190
+ var postal = $ ( this ) . attr ( 'postal' ) ;
191
+ var phone = $ ( this ) . attr ( 'phone' ) ;
192
+ var web = $ ( this ) . attr ( 'web' ) ;
193
+ web = web . replace ( "http://" , "" ) ;
194
+ var hours1 = $ ( this ) . attr ( 'hours1' ) ;
195
+ var hours2 = $ ( this ) . attr ( 'hours2' ) ;
196
+ var hours3 = $ ( this ) . attr ( 'hours3' ) ;
197
+
198
+ var distance = GeoCodeCalc . CalcDistance ( orig_lat , orig_lng , lat , lng , GeoCodeCalc . EarthRadiusInMiles ) ;
199
+
200
+ //Create the array
201
+ locationset [ i ] = new Array ( distance , name , lat , lng , address , address2 , city , state , postal , phone , web , hours1 , hours2 , hours3 ) ;
202
+
203
+ i ++ ;
204
+ } ) ;
205
+ }
152
206
153
207
//Sort the multi-dimensional array numerically
154
208
locationset . sort ( function ( a , b ) {
@@ -166,11 +220,12 @@ $.fn.storeLocator = function(options) {
166
220
//Create the map with jQuery
167
221
$ ( function ( ) {
168
222
169
- var storeName , storeAddress1 , storeAddress2 , storeCity , storeState , storeZip , storePhone , storeWeb , storeHours1 , storeHours2 , storeHours3 ;
223
+ var storeDistance , storeName , storeAddress1 , storeAddress2 , storeCity , storeState , storeZip , storePhone , storeWeb , storeHours1 , storeHours2 , storeHours3 ;
170
224
171
225
//Instead of repeating the same thing twice below
172
226
function create_store_variables ( loopcount )
173
227
{
228
+ storeDistance = locationset [ loopcount ] [ 0 ] ;
174
229
storeName = locationset [ loopcount ] [ 1 ] ;
175
230
storeAddress1 = locationset [ loopcount ] [ 4 ] ;
176
231
storeAddress2 = locationset [ loopcount ] [ 5 ] ;
@@ -182,6 +237,8 @@ $.fn.storeLocator = function(options) {
182
237
storeHours1 = locationset [ loopcount ] [ 11 ] ;
183
238
storeHours2 = locationset [ loopcount ] [ 12 ] ;
184
239
storeHours3 = locationset [ loopcount ] [ 13 ] ;
240
+
241
+ storeDistance = roundNumber ( storeDistance , 2 ) ;
185
242
}
186
243
187
244
//Google maps settings
@@ -253,13 +310,17 @@ $.fn.storeLocator = function(options) {
253
310
var letter = String . fromCharCode ( "A" . charCodeAt ( 0 ) + x ) ;
254
311
create_store_variables ( x ) ;
255
312
//This needs to happen outside the loop or there will be a closure problem with creating the infowindows attached to the list click
256
- listClick ( letter , marker , storeName , storeAddress1 , storeAddress2 , storeCity , storeState , storeZip , storePhone , storeWeb , storeHours1 , storeHours2 , storeHours3 ) ;
313
+ listClick ( storeDistance , letter , marker , storeName , storeAddress1 , storeAddress2 , storeCity , storeState , storeZip , storePhone , storeWeb , storeHours1 , storeHours2 , storeHours3 ) ;
257
314
258
315
} ) ;
259
316
260
- function listClick ( letter , marker , name , address1 , address2 , city , state , zip , phone , web , hours1 , hours2 , hours3 )
317
+ function listClick ( distance , letter , marker , name , address1 , address2 , city , state , zip , phone , web , hours1 , hours2 , hours3 )
261
318
{
262
- $ ( '<li />' ) . html ( "<div class=\"list-label\">" + letter + "<\/div><div class=\"list-details\"><div class=\"list-content\"><div class=\"loc-name\">" + storeName + "<\/div> <div class=\"loc-addr\">" + storeAddress1 + "<\/div> <div class=\"loc-addr2\">" + storeAddress2 + "<\/div> <div class=\"loc-addr3\">" + storeCity + ", " + storeState + " " + storeZip + "<\/div> <div class=\"loc-phone\">" + storePhone + "<\/div> <div class=\"loc-web\"><a href=\"http://" + storeWeb + "\" target=\"_blank\">" + storeWeb + "</a><\/div><\/div><\/div>" ) . click ( function ( ) {
319
+ var distLength ;
320
+ if ( distance <= 1 ) { distLength = "mile" ; }
321
+ else { distLength = "miles" ; }
322
+
323
+ $ ( '<li />' ) . html ( "<div class=\"list-label\">" + letter + "<\/div><div class=\"list-details\"><div class=\"list-content\"><div class=\"loc-name\">" + storeName + "<\/div> <div class=\"loc-addr\">" + storeAddress1 + "<\/div> <div class=\"loc-addr2\">" + storeAddress2 + "<\/div> <div class=\"loc-addr3\">" + storeCity + ", " + storeState + " " + storeZip + "<\/div> <div class=\"loc-phone\">" + storePhone + "<\/div> <div class=\"loc-web\"><a href=\"http://" + storeWeb + "\" target=\"_blank\">" + storeWeb + "</a><\/div><div class=\"loc-dist\">" + distance + " " + distLength + "<\/div><\/div><\/div>" ) . click ( function ( ) {
263
324
map . panTo ( marker . getPosition ( ) ) ;
264
325
var listLoc = "left" ;
265
326
if ( settings . bounceMarker == true )
0 commit comments