7
7
* https://github.com/derekeder/FusionTable-Map-Template/wiki/License
8
8
*
9
9
* Date: 12/10/2012
10
- *
10
+ *
11
11
*/
12
-
12
+
13
13
var MapsLib = MapsLib || { } ;
14
14
var MapsLib = {
15
-
15
+
16
16
//Setup section - put your Fusion Table details here
17
17
//Using the v1 Fusion Tables API. See https://developers.google.com/fusiontables/docs/v1/migration_guide for more info
18
-
18
+
19
19
//the encrypted Table ID of your Fusion Table (found under File => About)
20
20
//NOTE: numeric IDs will be depricated soon
21
- fusionTableId : "1m4Ez9xyTGfY2CU6O-UgEcPzlS0rnzLU93e4Faa0" ,
22
-
23
- //*New Fusion Tables Requirement* API key. found at https://code.google.com/apis/console/
24
- //*Important* this key is for demonstration purposes. please register your own.
25
- googleApiKey : "AIzaSyA3FQFrNr5W2OEVmuENqhb2MBB2JabdaOY" ,
26
-
27
- //name of the location column in your Fusion Table.
28
- //NOTE: if your location column name has spaces in it, surround it with single quotes
21
+ fusionTableId : "1m4Ez9xyTGfY2CU6O-UgEcPzlS0rnzLU93e4Faa0" ,
22
+
23
+ //*New Fusion Tables Requirement* API key. found at https://code.google.com/apis/console/
24
+ //*Important* this key is for demonstration purposes. please register your own.
25
+ googleApiKey : "AIzaSyA3FQFrNr5W2OEVmuENqhb2MBB2JabdaOY" ,
26
+
27
+ //name of the location column in your Fusion Table.
28
+ //NOTE: if your location column name has spaces in it, surround it with single quotes
29
29
//example: locationColumn: "'my location'",
30
- locationColumn : "geometry" ,
30
+ locationColumn : "geometry" ,
31
31
32
32
map_centroid : new google . maps . LatLng ( 41.8781136 , - 87.66677856445312 ) , //center that your map defaults to
33
33
locationScope : "chicago" , //geographical area appended to all address searches
34
34
recordName : "result" , //for showing number of results
35
- recordNamePlural : "results" ,
36
-
35
+ recordNamePlural : "results" ,
36
+
37
37
searchRadius : 805 , //in meters ~ 1/2 mile
38
38
defaultZoom : 11 , //zoom level when map is loaded (bigger is more zoomed in)
39
39
addrMarkerImage : 'http://derekeder.com/images/icons/blue-pushpin.png' ,
40
40
currentPinpoint : null ,
41
-
41
+
42
42
initialize : function ( ) {
43
43
$ ( "#result_count" ) . html ( "" ) ;
44
-
44
+
45
45
geocoder = new google . maps . Geocoder ( ) ;
46
46
var myOptions = {
47
47
zoom : MapsLib . defaultZoom ,
48
48
center : MapsLib . map_centroid ,
49
49
mapTypeId : google . maps . MapTypeId . ROADMAP
50
50
} ;
51
51
map = new google . maps . Map ( $ ( "#map_canvas" ) [ 0 ] , myOptions ) ;
52
-
52
+
53
+ // maintains map centerpoint for responsive design
54
+ google . maps . event . addDomListener ( map , 'idle' , function ( ) {
55
+ MapsLib . calculateCenter ( ) ;
56
+ } ) ;
57
+
58
+ google . maps . event . addDomListener ( window , 'resize' , function ( ) {
59
+ map . setCenter ( MapsLib . map_centroid ) ;
60
+ } ) ;
61
+
53
62
MapsLib . searchrecords = null ;
54
-
63
+
55
64
//reset filters
56
65
$ ( "#search_address" ) . val ( MapsLib . convertToPlainString ( $ . address . parameter ( 'address' ) ) ) ;
57
66
var loadRadius = MapsLib . convertToPlainString ( $ . address . parameter ( 'radius' ) ) ;
58
67
if ( loadRadius != "" ) $ ( "#search_radius" ) . val ( loadRadius ) ;
59
68
else $ ( "#search_radius" ) . val ( MapsLib . searchRadius ) ;
60
69
$ ( ":checkbox" ) . attr ( "checked" , "checked" ) ;
61
70
$ ( "#result_count" ) . hide ( ) ;
62
-
71
+
63
72
//run the default search
64
73
MapsLib . doSearch ( ) ;
65
74
} ,
66
-
75
+
67
76
doSearch : function ( location ) {
68
77
MapsLib . clearSearch ( ) ;
69
78
var address = $ ( "#search_address" ) . val ( ) ;
70
79
MapsLib . searchRadius = $ ( "#search_radius" ) . val ( ) ;
71
80
72
81
var whereClause = MapsLib . locationColumn + " not equal to ''" ;
73
-
82
+
74
83
//-----custom filters-------
75
-
84
+
76
85
//-------end of custom filters--------
77
-
86
+
78
87
if ( address != "" ) {
79
88
if ( address . toLowerCase ( ) . indexOf ( MapsLib . locationScope ) == - 1 )
80
89
address = address + " " + MapsLib . locationScope ;
81
-
90
+
82
91
geocoder . geocode ( { 'address' : address } , function ( results , status ) {
83
92
if ( status == google . maps . GeocoderStatus . OK ) {
84
93
MapsLib . currentPinpoint = results [ 0 ] . geometry . location ;
85
-
94
+
86
95
$ . address . parameter ( 'address' , encodeURIComponent ( address ) ) ;
87
96
$ . address . parameter ( 'radius' , encodeURIComponent ( MapsLib . searchRadius ) ) ;
88
97
map . setCenter ( MapsLib . currentPinpoint ) ;
89
98
map . setZoom ( 14 ) ;
90
-
99
+
91
100
MapsLib . addrMarker = new google . maps . Marker ( {
92
- position : MapsLib . currentPinpoint ,
93
- map : map ,
101
+ position : MapsLib . currentPinpoint ,
102
+ map : map ,
94
103
icon : MapsLib . addrMarkerImage ,
95
104
animation : google . maps . Animation . DROP ,
96
105
title :address
97
106
} ) ;
98
-
107
+
99
108
whereClause += " AND ST_INTERSECTS(" + MapsLib . locationColumn + ", CIRCLE(LATLNG" + MapsLib . currentPinpoint . toString ( ) + "," + MapsLib . searchRadius + "))" ;
100
-
109
+
101
110
MapsLib . drawSearchRadiusCircle ( MapsLib . currentPinpoint ) ;
102
111
MapsLib . submitSearch ( whereClause , map , MapsLib . currentPinpoint ) ;
103
- }
112
+ }
104
113
else {
105
114
alert ( "We could not find your address: " + status ) ;
106
115
}
@@ -110,12 +119,12 @@ var MapsLib = {
110
119
MapsLib . submitSearch ( whereClause , map ) ;
111
120
}
112
121
} ,
113
-
122
+
114
123
submitSearch : function ( whereClause , map , location ) {
115
124
//get using all filters
116
125
//NOTE: styleId and templateId are recently added attributes to load custom marker styles and info windows
117
126
//you can find your Ids inside the link generated by the 'Publish' option in Fusion Tables
118
- //for more details, see https://developers.google.com/fusiontables/docs/v1/using#WorkingStyles
127
+ //for more details, see https://developers.google.com/fusiontables/docs/v1/using#WorkingStyles
119
128
120
129
MapsLib . searchrecords = new google . maps . FusionTablesLayer ( {
121
130
query : {
@@ -129,20 +138,20 @@ var MapsLib = {
129
138
MapsLib . searchrecords . setMap ( map ) ;
130
139
MapsLib . getCount ( whereClause ) ;
131
140
} ,
132
-
141
+
133
142
clearSearch : function ( ) {
134
143
if ( MapsLib . searchrecords != null )
135
144
MapsLib . searchrecords . setMap ( null ) ;
136
145
if ( MapsLib . addrMarker != null )
137
- MapsLib . addrMarker . setMap ( null ) ;
146
+ MapsLib . addrMarker . setMap ( null ) ;
138
147
if ( MapsLib . searchRadiusCircle != null )
139
148
MapsLib . searchRadiusCircle . setMap ( null ) ;
140
149
} ,
141
-
150
+
142
151
findMe : function ( ) {
143
152
// Try W3C Geolocation (Preferred)
144
153
var foundLocation ;
145
-
154
+
146
155
if ( navigator . geolocation ) {
147
156
navigator . geolocation . getCurrentPosition ( function ( position ) {
148
157
foundLocation = new google . maps . LatLng ( position . coords . latitude , position . coords . longitude ) ;
@@ -153,7 +162,7 @@ var MapsLib = {
153
162
alert ( "Sorry, we could not find your location." ) ;
154
163
}
155
164
} ,
156
-
165
+
157
166
addrFromLatLng : function ( latLngPoint ) {
158
167
geocoder . geocode ( { 'latLng' : latLngPoint } , function ( results , status ) {
159
168
if ( status == google . maps . GeocoderStatus . OK ) {
@@ -167,7 +176,7 @@ var MapsLib = {
167
176
}
168
177
} ) ;
169
178
} ,
170
-
179
+
171
180
drawSearchRadiusCircle : function ( point ) {
172
181
var circleOptions = {
173
182
strokeColor : "#4b58a6" ,
@@ -183,13 +192,13 @@ var MapsLib = {
183
192
} ;
184
193
MapsLib . searchRadiusCircle = new google . maps . Circle ( circleOptions ) ;
185
194
} ,
186
-
195
+
187
196
query : function ( selectColumns , whereClause , callback ) {
188
197
var queryStr = [ ] ;
189
198
queryStr . push ( "SELECT " + selectColumns ) ;
190
199
queryStr . push ( " FROM " + MapsLib . fusionTableId ) ;
191
200
queryStr . push ( " WHERE " + whereClause ) ;
192
-
201
+
193
202
var sql = encodeURIComponent ( queryStr . join ( " " ) ) ;
194
203
$ . ajax ( { url : "https://www.googleapis.com/fusiontables/v1/query?sql=" + sql + "&callback=" + callback + "&key=" + MapsLib . googleApiKey , dataType : "jsonp" } ) ;
195
204
} ,
@@ -205,18 +214,18 @@ var MapsLib = {
205
214
}
206
215
}
207
216
} ,
208
-
217
+
209
218
getCount : function ( whereClause ) {
210
219
var selectColumns = "Count()" ;
211
220
MapsLib . query ( selectColumns , whereClause , "MapsLib.displaySearchCount" ) ;
212
221
} ,
213
-
214
- displaySearchCount : function ( json ) {
222
+
223
+ displaySearchCount : function ( json ) {
215
224
MapsLib . handleError ( json ) ;
216
225
var numRows = 0 ;
217
226
if ( json [ "rows" ] != null )
218
227
numRows = json [ "rows" ] [ 0 ] ;
219
-
228
+
220
229
var name = MapsLib . recordNamePlural ;
221
230
if ( numRows == 1 )
222
231
name = MapsLib . recordName ;
@@ -225,7 +234,7 @@ var MapsLib = {
225
234
} ) ;
226
235
$ ( "#result_count" ) . fadeIn ( ) ;
227
236
} ,
228
-
237
+
229
238
addCommas : function ( nStr ) {
230
239
nStr += '' ;
231
240
x = nStr . split ( '.' ) ;
@@ -237,10 +246,15 @@ var MapsLib = {
237
246
}
238
247
return x1 + x2 ;
239
248
} ,
240
-
249
+
250
+ // maintains map centerpoint for responsive design
251
+ calculateCenter : function ( ) {
252
+ center = map . getCenter ( ) ;
253
+ } ,
254
+
241
255
//converts a slug or query string in to readable text
242
256
convertToPlainString : function ( text ) {
243
257
if ( text == undefined ) return '' ;
244
258
return decodeURIComponent ( text ) ;
245
259
}
246
- }
260
+ }
0 commit comments