1
1
/*
2
- * storeLocator v1.2 - jQuery store locator plugin
2
+ * storeLocator v1.3 - 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
@@ -27,7 +27,8 @@ $.fn.storeLocator = function(options) {
27
27
'defaultLoc' : false ,
28
28
'defaultLat' : '' ,
29
29
'defaultLng' : '' ,
30
- 'jsonData' : false
30
+ 'jsonData' : false ,
31
+ 'autoGeocode' : false
31
32
} , options ) ;
32
33
33
34
return this . each ( function ( ) {
@@ -48,12 +49,7 @@ $.fn.storeLocator = function(options) {
48
49
$this . hide ( ) ;
49
50
}
50
51
51
- if ( settings . defaultLoc == true )
52
- {
53
- mapping ( settings . defaultLat , settings . defaultLng ) ;
54
- }
55
-
56
- var userinput , olat , olng , marker , letter , geocoder , storenum ;
52
+ var userinput , olat , olng , marker , letter , storenum ;
57
53
var locationset = new Array ( ) ;
58
54
59
55
//Calculate geocode distance functions - you could use Google's distance service instead
@@ -69,8 +65,8 @@ $.fn.storeLocator = function(options) {
69
65
} ;
70
66
71
67
//Geocode function for the origin location
72
- geocoder = new google . maps . Geocoder ( ) ;
73
68
function GoogleGeocode ( ) {
69
+ geocoder = new google . maps . Geocoder ( ) ;
74
70
this . geocode = function ( address , callbackFunction ) {
75
71
geocoder . geocode ( { 'address' : address } , function ( results , status ) {
76
72
if ( status == google . maps . GeocoderStatus . OK ) {
@@ -87,6 +83,75 @@ $.fn.storeLocator = function(options) {
87
83
} ;
88
84
}
89
85
86
+ //Reverse geocode to get address for automatic options needed for directions link
87
+ function ReverseGoogleGeocode ( )
88
+ {
89
+ geocoder = new google . maps . Geocoder ( ) ;
90
+ this . geocode = function ( latlng , callbackFunction ) {
91
+ geocoder . geocode ( { 'latLng' : latlng } , function ( results , status ) {
92
+ if ( status == google . maps . GeocoderStatus . OK ) {
93
+ if ( results [ 0 ] ) {
94
+ var result = { } ;
95
+ result . address = results [ 0 ] . formatted_address ;
96
+ callbackFunction ( result ) ;
97
+ }
98
+ } else {
99
+ alert ( "Geocode was not successful for the following reason: " + status ) ;
100
+ callbackFunction ( null ) ;
101
+ }
102
+ } ) ;
103
+ } ;
104
+ }
105
+
106
+ //If a default location is set
107
+ if ( settings . defaultLoc == true )
108
+ {
109
+ //The address needs to be determined for the directions link
110
+ var r = new ReverseGoogleGeocode ( ) ;
111
+ var latlng = new google . maps . LatLng ( settings . defaultLat , settings . defaultLng ) ;
112
+ r . geocode ( latlng , function ( data ) {
113
+ if ( data != null ) {
114
+ var originAddress = data . address ;
115
+ mapping ( settings . defaultLat , settings . defaultLng , originAddress ) ;
116
+ } else {
117
+ //Unable to geocode
118
+ alert ( 'Unable to find address' ) ;
119
+ }
120
+ } ) ;
121
+ }
122
+
123
+ //HTML5 geolocation API option
124
+ if ( settings . autoGeocode == true )
125
+ {
126
+ if ( navigator . geolocation )
127
+ {
128
+ navigator . geolocation . getCurrentPosition ( autoGeocode_query , autoGeocode_error ) ;
129
+ }
130
+ }
131
+
132
+ //If location is detected automatically
133
+ function autoGeocode_query ( position )
134
+ {
135
+ //The address needs to be determined for the directions link
136
+ var r = new ReverseGoogleGeocode ( ) ;
137
+ var latlng = new google . maps . LatLng ( position . coords . latitude , position . coords . longitude ) ;
138
+ r . geocode ( latlng , function ( data ) {
139
+ if ( data != null ) {
140
+ var originAddress = data . address ;
141
+ mapping ( position . coords . latitude , position . coords . longitude , originAddress ) ;
142
+ } else {
143
+ //Unable to geocode
144
+ alert ( 'Unable to find address' ) ;
145
+ }
146
+ } ) ;
147
+ }
148
+
149
+ function autoGeocode_error ( error )
150
+ {
151
+ //If automatic detection doesn't work show an error
152
+ alert ( "Automatic location detection failed. Please fill in your address or zip code." ) ;
153
+ }
154
+
90
155
//Used to round miles to display
91
156
function roundNumber ( num , dec )
92
157
{
@@ -115,7 +180,7 @@ $.fn.storeLocator = function(options) {
115
180
if ( data != null ) {
116
181
olat = data . latitude ;
117
182
olng = data . longitude ;
118
- mapping ( olat , olng ) ;
183
+ mapping ( olat , olng , userinput ) ;
119
184
} else {
120
185
//Unable to geocode
121
186
alert ( 'ERROR! Unable to geocode address' ) ;
@@ -128,7 +193,7 @@ $.fn.storeLocator = function(options) {
128
193
129
194
130
195
//Now all the mapping stuff
131
- function mapping ( orig_lat , orig_lng ) {
196
+ function mapping ( orig_lat , orig_lng , origin ) {
132
197
$ ( function ( ) {
133
198
134
199
var dataType ;
@@ -320,7 +385,10 @@ $.fn.storeLocator = function(options) {
320
385
if ( distance <= 1 ) { distLength = "mile" ; }
321
386
else { distLength = "miles" ; }
322
387
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 ( ) {
388
+ var fullAddress = address1 + " " + address2 + ", " + city + ", " + state + " " + zip ;
389
+ var distLink = "<a href=\"http://maps.google.com/maps?saddr=" + origin + "&daddr=" + fullAddress + "\" target=\"_blank\">Directions<\/a>" ;
390
+
391
+ $ ( '<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 class=\"loc-directions\">" + distLink + "<\/div><\/div><\/div>" ) . click ( function ( ) {
324
392
map . panTo ( marker . getPosition ( ) ) ;
325
393
var listLoc = "left" ;
326
394
if ( settings . bounceMarker == true )
0 commit comments