Skip to content
This repository was archived by the owner on May 24, 2021. It is now read-only.

Commit 0a986e5

Browse files
committed
Merge pull request #11 from chrislkeller/map_center_on_resize
adds function to maintain map centerpoint when template is resized
2 parents f8f11d1 + 79512c5 commit 0a986e5

File tree

1 file changed

+63
-49
lines changed

1 file changed

+63
-49
lines changed

source/maps_lib.js

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,100 +7,109 @@
77
* https://github.com/derekeder/FusionTable-Map-Template/wiki/License
88
*
99
* Date: 12/10/2012
10-
*
10+
*
1111
*/
12-
12+
1313
var MapsLib = MapsLib || {};
1414
var MapsLib = {
15-
15+
1616
//Setup section - put your Fusion Table details here
1717
//Using the v1 Fusion Tables API. See https://developers.google.com/fusiontables/docs/v1/migration_guide for more info
18-
18+
1919
//the encrypted Table ID of your Fusion Table (found under File => About)
2020
//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
2929
//example: locationColumn: "'my location'",
30-
locationColumn: "geometry",
30+
locationColumn: "geometry",
3131

3232
map_centroid: new google.maps.LatLng(41.8781136, -87.66677856445312), //center that your map defaults to
3333
locationScope: "chicago", //geographical area appended to all address searches
3434
recordName: "result", //for showing number of results
35-
recordNamePlural: "results",
36-
35+
recordNamePlural: "results",
36+
3737
searchRadius: 805, //in meters ~ 1/2 mile
3838
defaultZoom: 11, //zoom level when map is loaded (bigger is more zoomed in)
3939
addrMarkerImage: 'http://derekeder.com/images/icons/blue-pushpin.png',
4040
currentPinpoint: null,
41-
41+
4242
initialize: function() {
4343
$( "#result_count" ).html("");
44-
44+
4545
geocoder = new google.maps.Geocoder();
4646
var myOptions = {
4747
zoom: MapsLib.defaultZoom,
4848
center: MapsLib.map_centroid,
4949
mapTypeId: google.maps.MapTypeId.ROADMAP
5050
};
5151
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+
5362
MapsLib.searchrecords = null;
54-
63+
5564
//reset filters
5665
$("#search_address").val(MapsLib.convertToPlainString($.address.parameter('address')));
5766
var loadRadius = MapsLib.convertToPlainString($.address.parameter('radius'));
5867
if (loadRadius != "") $("#search_radius").val(loadRadius);
5968
else $("#search_radius").val(MapsLib.searchRadius);
6069
$(":checkbox").attr("checked", "checked");
6170
$("#result_count").hide();
62-
71+
6372
//run the default search
6473
MapsLib.doSearch();
6574
},
66-
75+
6776
doSearch: function(location) {
6877
MapsLib.clearSearch();
6978
var address = $("#search_address").val();
7079
MapsLib.searchRadius = $("#search_radius").val();
7180

7281
var whereClause = MapsLib.locationColumn + " not equal to ''";
73-
82+
7483
//-----custom filters-------
75-
84+
7685
//-------end of custom filters--------
77-
86+
7887
if (address != "") {
7988
if (address.toLowerCase().indexOf(MapsLib.locationScope) == -1)
8089
address = address + " " + MapsLib.locationScope;
81-
90+
8291
geocoder.geocode( { 'address': address}, function(results, status) {
8392
if (status == google.maps.GeocoderStatus.OK) {
8493
MapsLib.currentPinpoint = results[0].geometry.location;
85-
94+
8695
$.address.parameter('address', encodeURIComponent(address));
8796
$.address.parameter('radius', encodeURIComponent(MapsLib.searchRadius));
8897
map.setCenter(MapsLib.currentPinpoint);
8998
map.setZoom(14);
90-
99+
91100
MapsLib.addrMarker = new google.maps.Marker({
92-
position: MapsLib.currentPinpoint,
93-
map: map,
101+
position: MapsLib.currentPinpoint,
102+
map: map,
94103
icon: MapsLib.addrMarkerImage,
95104
animation: google.maps.Animation.DROP,
96105
title:address
97106
});
98-
107+
99108
whereClause += " AND ST_INTERSECTS(" + MapsLib.locationColumn + ", CIRCLE(LATLNG" + MapsLib.currentPinpoint.toString() + "," + MapsLib.searchRadius + "))";
100-
109+
101110
MapsLib.drawSearchRadiusCircle(MapsLib.currentPinpoint);
102111
MapsLib.submitSearch(whereClause, map, MapsLib.currentPinpoint);
103-
}
112+
}
104113
else {
105114
alert("We could not find your address: " + status);
106115
}
@@ -110,12 +119,12 @@ var MapsLib = {
110119
MapsLib.submitSearch(whereClause, map);
111120
}
112121
},
113-
122+
114123
submitSearch: function(whereClause, map, location) {
115124
//get using all filters
116125
//NOTE: styleId and templateId are recently added attributes to load custom marker styles and info windows
117126
//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
119128

120129
MapsLib.searchrecords = new google.maps.FusionTablesLayer({
121130
query: {
@@ -129,20 +138,20 @@ var MapsLib = {
129138
MapsLib.searchrecords.setMap(map);
130139
MapsLib.getCount(whereClause);
131140
},
132-
141+
133142
clearSearch: function() {
134143
if (MapsLib.searchrecords != null)
135144
MapsLib.searchrecords.setMap(null);
136145
if (MapsLib.addrMarker != null)
137-
MapsLib.addrMarker.setMap(null);
146+
MapsLib.addrMarker.setMap(null);
138147
if (MapsLib.searchRadiusCircle != null)
139148
MapsLib.searchRadiusCircle.setMap(null);
140149
},
141-
150+
142151
findMe: function() {
143152
// Try W3C Geolocation (Preferred)
144153
var foundLocation;
145-
154+
146155
if(navigator.geolocation) {
147156
navigator.geolocation.getCurrentPosition(function(position) {
148157
foundLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
@@ -153,7 +162,7 @@ var MapsLib = {
153162
alert("Sorry, we could not find your location.");
154163
}
155164
},
156-
165+
157166
addrFromLatLng: function(latLngPoint) {
158167
geocoder.geocode({'latLng': latLngPoint}, function(results, status) {
159168
if (status == google.maps.GeocoderStatus.OK) {
@@ -167,7 +176,7 @@ var MapsLib = {
167176
}
168177
});
169178
},
170-
179+
171180
drawSearchRadiusCircle: function(point) {
172181
var circleOptions = {
173182
strokeColor: "#4b58a6",
@@ -183,13 +192,13 @@ var MapsLib = {
183192
};
184193
MapsLib.searchRadiusCircle = new google.maps.Circle(circleOptions);
185194
},
186-
195+
187196
query: function(selectColumns, whereClause, callback) {
188197
var queryStr = [];
189198
queryStr.push("SELECT " + selectColumns);
190199
queryStr.push(" FROM " + MapsLib.fusionTableId);
191200
queryStr.push(" WHERE " + whereClause);
192-
201+
193202
var sql = encodeURIComponent(queryStr.join(" "));
194203
$.ajax({url: "https://www.googleapis.com/fusiontables/v1/query?sql="+sql+"&callback="+callback+"&key="+MapsLib.googleApiKey, dataType: "jsonp"});
195204
},
@@ -205,18 +214,18 @@ var MapsLib = {
205214
}
206215
}
207216
},
208-
217+
209218
getCount: function(whereClause) {
210219
var selectColumns = "Count()";
211220
MapsLib.query(selectColumns, whereClause,"MapsLib.displaySearchCount");
212221
},
213-
214-
displaySearchCount: function(json) {
222+
223+
displaySearchCount: function(json) {
215224
MapsLib.handleError(json);
216225
var numRows = 0;
217226
if (json["rows"] != null)
218227
numRows = json["rows"][0];
219-
228+
220229
var name = MapsLib.recordNamePlural;
221230
if (numRows == 1)
222231
name = MapsLib.recordName;
@@ -225,7 +234,7 @@ var MapsLib = {
225234
});
226235
$( "#result_count" ).fadeIn();
227236
},
228-
237+
229238
addCommas: function(nStr) {
230239
nStr += '';
231240
x = nStr.split('.');
@@ -237,10 +246,15 @@ var MapsLib = {
237246
}
238247
return x1 + x2;
239248
},
240-
249+
250+
// maintains map centerpoint for responsive design
251+
calculateCenter: function() {
252+
center = map.getCenter();
253+
},
254+
241255
//converts a slug or query string in to readable text
242256
convertToPlainString: function(text) {
243257
if (text == undefined) return '';
244258
return decodeURIComponent(text);
245259
}
246-
}
260+
}

0 commit comments

Comments
 (0)