forked from fridek/gmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.gmap.js
381 lines (329 loc) · 16.4 KB
/
jquery.gmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* jQuery gMap v3
*
* @url http://www.smashinglabs.pl/gmap
* @author Sebastian Poreba <[email protected]>
* @version 3.1.0
* @date 23.04.2011
*/
/*jslint white: true, undef: true, regexp: true, plusplus: true, bitwise: true, newcap: true, strict: true, devel: true, maxerr: 50, indent: 4 */
/*global window, jQuery, $, google, $googlemaps */
(function ($) {
// global google maps objects
var $googlemaps = google.maps,
$geocoder = new $googlemaps.Geocoder(),
opts = {},
$markersToLoad = 0,
methods = {
init: function (options) {
// Build main options before element iteration
opts = $.extend({}, $.fn.gMap.defaults, options);
// recover icon array
for(var k in $.fn.gMap.defaults.icon) {
if(!opts.icon[k]) {
opts.icon[k] = $.fn.gMap.defaults.icon[k];
}
}
// Iterate through each element
return this.each(function () {
var $this = $(this),
center = methods._getMapCenter.apply($this, []),
mapOptions = {
zoom: opts.zoom,
center: center,
mapTypeControl: opts.mapTypeControl,
zoomControl: opts.zoomControl,
panControl : opts.panControl,
scaleControl : opts.scaleControl,
streetViewControl: opts.streetViewControl,
mapTypeId: opts.maptype,
scrollwheel: opts.scrollwheel,
maxZoom: opts.maxZoom,
minZoom: opts.minZoom
};
if (opts.log) {console.log('map center is:');}
if (opts.log) {console.log(center);}
// Create map and set initial options
var $gmap = new $googlemaps.Map(this, mapOptions);
$this.data("$gmap", $gmap);
$this.data('gmap', {
'opts': opts,
'gmap': $gmap,
'markers': [],
'infoWindow': null
});
// Check for map controls
if (opts.controls.length !== 0) {
// Add custom map controls
for (var i = 0; i < opts.controls.length; i+= 1) {
$gmap.controls[opts.controls[i].pos].push(opts.controls[i].div);
}
}
if (opts.markers.length !== 0) {
// Loop through marker array
for (var i = 0; i < opts.markers.length; i+= 1) {
methods.addMarker.apply($this,[opts.markers[i]]);
}
}
methods._onComplete.apply($this, []);
});
},
_onComplete: function () {
var $data = this.data('gmap'),
that = this;
if($markersToLoad !== 0) {
window.setTimeout(function () { methods._onComplete.apply(that, [])}, 1000);
return;
}
$data.opts.onComplete();
},
_setMapCenter: function (center) {
if (opts.log) {console.log('delayed setMapCenter called');}
var $data = this.data('gmap');
if ($data.gmap !== undefined) {
$data.gmap.setCenter(center);
} else {
var that = this;
window.setTimeout(function() {methods._setMapCenter.apply(that,[center]);}, 500);
}
},
/**
* Priorities order:
* - latitude & longitude in options
* - address in options
* - latitude & longitude of first marker having it
* - address of first marker having it
* - failsafe (0,0)
*
* Note: with geocoding returned value is (0,0) and callback sets map center. It's not very nice nor efficient.
* It is quite good idea to use only first option
*/
_getMapCenter: function () {
// Create new object to geocode addresses
var center, that = this; // 'that' scope fix in geocoding
if (opts.latitude && opts.longitude) {
// lat & lng available, return
center = new $googlemaps.LatLng(opts.latitude, opts.longitude);
return center;
} else {
center = new $googlemaps.LatLng(0, 0);
}
// Check for address to center on
if (opts.address) {
// Get coordinates for given address and center the map
$geocoder.geocode(
{address: opts.address},
function(result, status) {
if (status === google.maps.GeocoderStatus.OK) {
methods._setMapCenter.apply(that,[result[0].geometry.location]);
} else {
if (opts.log) {console.log("Geocode was not successful for the following reason: " + status);}
}
});
return center;
}
// Check for a marker to center on (if no coordinates given)
if ($.isArray(opts.markers) && opts.markers.length > 0) {
var selectedToCenter = null;
for (var i = 0; i < opts.markers.length; i+= 1) {
if (opts.markers[i].latitude && opts.markers[i].longitude) {
selectedToCenter = opts.markers[i];
break;
}
if (opts.markers[i].address) {
selectedToCenter = opts.markers[i];
}
}
// failed to find any reasonable marker (it's quite impossible BTW)
if (selectedToCenter === null) {
return center;
}
if (selectedToCenter.latitude && selectedToCenter.longitude) {
return new $googlemaps.LatLng(selectedToCenter.latitude, selectedToCenter.longitude);
}
// Check if the marker has an address
if (selectedToCenter.address) {
// Get the coordinates for given marker address and center
$geocoder.geocode(
{address: selectedToCenter.address},
function(result, status) {
if (status === google.maps.GeocoderStatus.OK) {
methods._setMapCenter.apply(that,[result[0].geometry.location]);
} else {
if (opts.log) {console.log("Geocode was not successful for the following reason: " + status);}
}
});
}
}
return center;
},
processMarker: function (marker, gicon, gshadow, location) {
var $data = this.data('gmap'),
$gmap = $data.gmap;
if (location === undefined) {
location = new $googlemaps.LatLng(marker.latitude, marker.longitude);
}
if (!gicon) {
// Set icon properties from global options
var _gicon = {
image: opts.icon.image,
iconSize: new $googlemaps.Size(opts.icon.iconsize[0], opts.icon.iconsize[1]),
iconAnchor: new $googlemaps.Point(opts.icon.iconanchor[0], opts.icon.iconanchor[1]),
infoWindowAnchor: new $googlemaps.Size(opts.icon.infowindowanchor[0], opts.icon.infowindowanchor[1])
};
gicon = new $googlemaps.MarkerImage(_gicon.image, _gicon.iconSize, null, _gicon.iconAnchor);
}
if (!gshadow) {
var _gshadow = {
image: opts.icon.shadow,
iconSize: new $googlemaps.Size(opts.icon.shadowsize[0], opts.icon.shadowsize[1]),
anchor: (_gicon && _gicon.iconAnchor)?_gicon.iconAnchor:new $googlemaps.Point(opts.icon.iconanchor[0], opts.icon.iconanchor[1])
};
}
var gmarker = new $googlemaps.Marker({
position: location,
icon: gicon,
title: marker.title,
map: $gmap
});
gmarker.setShadow(gshadow);
$data.markers.push(gmarker);
// Set HTML and check if info window should be opened
var infoWindow;
if (marker.html) {
var infoOpts = {
content: opts.html_prepend + marker.html + opts.html_append,
pixelOffset: marker.infoWindowAnchor
};
if (opts.log) {console.log('setup popup with data');}
if (opts.log) {console.log(infoOpts);}
infoWindow = new $googlemaps.InfoWindow(infoOpts);
$googlemaps.event.addListener(gmarker, 'click', function() {
if (opts.log) {console.log('opening popup ' + marker.html);}
if (opts.singleInfoWindow && $data.infoWindow) {$data.infoWindow.close();}
infoWindow.open($gmap, gmarker);
$data.infoWindow = infoWindow;
});
}
if (marker.html && marker.popup) {
if (opts.log) {console.log('opening popup ' + marker.html);}
infoWindow.open($gmap, gmarker);
}
},
_geocodeMarker: function (marker, gicon, gshadow) {
$markersToLoad += 1;
var that = this;
$geocoder.geocode({'address': marker.address}, function(results, status) {
$markersToLoad -= 1;
if (status === $googlemaps.GeocoderStatus.OK) {
methods.processMarker.apply( that, [marker, gicon, gshadow, results[0].geometry.location] );
} else {
if (opts.log) {console.log("Geocode was not successful for the following reason: " + status);}
}
});
},
addMarker: function (marker) {
if (opts.log) {console.log("putting marker at " + marker.latitude + ', ' + marker.longitude + " with address " + marker.address + " and html " + marker.html);}
// Create new icon
// Set icon properties from global options
var _gicon = {
image: opts.icon.image,
iconSize: new $googlemaps.Size(opts.icon.iconsize[0], opts.icon.iconsize[1]),
iconAnchor: new $googlemaps.Point(opts.icon.iconanchor[0], opts.icon.iconanchor[1]),
infoWindowAnchor: new $googlemaps.Size(opts.icon.infowindowanchor[0], opts.icon.infowindowanchor[1])
},
_gshadow = {
image: opts.icon.shadow,
iconSize: new $googlemaps.Size(opts.icon.shadowsize[0], opts.icon.shadowsize[1]),
anchor: _gicon.iconAnchor
};
// not very nice, but useful
marker.infoWindowAnchor = _gicon.infoWindowAnchor;
if (marker.icon) {
// Overwrite global options
if (marker.icon.image) { _gicon.image = marker.icon.image; }
if (marker.icon.iconsize) { _gicon.iconSize = new $googlemaps.Size(marker.icon.iconsize[0], marker.icon.iconsize[1]); }
if (marker.icon.iconanchor) { _gicon.iconAnchor = new $googlemaps.Point(marker.icon.iconanchor[0], marker.icon.iconanchor[1]); }
if (marker.icon.infowindowanchor) { _gicon.infoWindowAnchor = new $googlemaps.Size(marker.icon.infowindowanchor[0], marker.icon.infowindowanchor[1]); }
if (marker.icon.shadow) { _gshadow.image = marker.icon.shadow; }
if (marker.icon.shadowsize) { _gshadow.iconSize = new $googlemaps.Size(marker.icon.shadowsize[0], marker.icon.shadowsize[1]); }
}
var gicon = new $googlemaps.MarkerImage(_gicon.image, _gicon.iconSize, null, _gicon.iconAnchor);
var gshadow = new $googlemaps.MarkerImage( _gshadow.image,_gshadow.iconSize, null, _gshadow.anchor);
// Check if address is available
if (marker.address) {
// Check for reference to the marker's address
if (marker.html === '_address') {
marker.html = marker.address;
}
if (marker.title == '_address') {
marker.title = marker.address;
}
if (opts.log) {console.log('geocoding marker: ' + marker.address);}
// Get the point for given address
methods._geocodeMarker.apply( this, [marker, gicon, gshadow] );
}
else {
// Check for reference to the marker's latitude/longitude
if (marker.html === '_latlng') {
marker.html = marker.latitude + ', ' + marker.longitude;
}
if (marker.title == '_latlng') {
marker.title = marker.latitude + ', ' + marker.longitude;
}
// Create marker
var gpoint = new $googlemaps.LatLng(marker.latitude, marker.longitude);
methods.processMarker.apply(this, [marker, gicon, gshadow, gpoint] );
}
},
removeAllMarkers: function () {
var markers = this.data('gmap').markers, i;
for(i = 0; i < markers.length; i += 1) {
markers[i].setMap(null);
}
markers = [];
}
};
// Main plugin function
$.fn.gMap = function (method) {
// Method calling logic
if (methods[method]) {
return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this,arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.gmap');
}
};
// Default settings
$.fn.gMap.defaults = {
log: false,
address: '',
latitude: null,
longitude: null,
zoom: 3,
maxZoom: null,
minZoom: null,
markers: [],
controls: {},
scrollwheel: true,
maptype: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
zoomControl: true,
panControl: false,
scaleControl: false,
streetViewControl: true,
singleInfoWindow: true,
html_prepend: '<div class="gmap_marker">',
html_append: '</div>',
icon: {
image: "http://www.google.com/mapfiles/marker.png",
iconsize: [20, 34],
iconanchor: [9, 34],
infowindowanchor: [9, 2],
shadow: "http://www.google.com/mapfiles/shadow50.png",
shadowsize: [37, 34]
},
onComplete: function() {}
};
}(jQuery));