-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.bmap.1.3.1.js
More file actions
562 lines (523 loc) · 18.6 KB
/
Copy pathjquery.bmap.1.3.1.js
File metadata and controls
562 lines (523 loc) · 18.6 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
bMap - © 2010 Darren Dignam
jquery.bmap.1.3.1.js - use with google v3 maps api
darren.dignam@blocsoft.com
http://www.blocsoft.com/bMap
Released under the GPL License
http://www.gnu.org/licenses/gpl-3.0.txt
*/
/*
This plugin requires the Google Maps API and jQuery (1.2.3+) to be loaded.
This plugin does not try to wrap the entire Google Maps API. I created this to aid marker management, AJAX overlays, and custom icons.
There have been some issues with multiple maps on one page. Please let me know of any bugs you find. For lots of maps, perhaps use iFrames, or try jMap.
*/
/*
The jQuery extending function
options:
mapCenter array latitude and logitude
mapZoom integer starting zoom
mapCanvas string the ID of the div to render the map
mapSidebar string the ID of the div to render a clickable sidebar of the markers
mapLayerbar string the ID of the div to render a clickable sidebar of the layers visible on the map
mapType integer the type of map tiles to load (G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP) refer to the google docs
loadMsg string message shown on map during AJAX operations
markers object bMap markers object, used to pre-draw some markers on the map
icons array array of GIcon objects
*/
(function($){
$.fn.bMap = function(options) {
eachOptions = options;
return this.each(function() {
obj = $(this);
var defaults = {
mapCenter: [51,0],
mapZoom: 1,
mapCanvas: obj.attr('id'),
mapSidebar: "none",
mapLayerbar: "none",
mapType: google.maps.MapTypeId.ROADMAP,
loadMsg: "<h2>Loading...</h2>"
};
var thisOptions = $.extend(defaults, eachOptions);
obj.data("bMap", new bMap(thisOptions) );
});
return this;
};
})(jQuery);
/*
The bMap object constructor
options:
mapCenter array latitude and logitude
mapZoom integer starting zoom
mapCanvas string the ID of the div to render the map
mapSidebar string the ID of the div to render a clickable sidebar of the markers
mapLayerbar string the ID of the div to render a clickable sidebar of the layers visible on the map
mapType integer the type of map tiles to load (G_NORMAL_MAP G_SATELLITE_MAP G_HYBRID_MAP) refer to the google docs
loadMsg string message shown on map during AJAX operations
markers object bMap markers object, used to pre-draw some markers on the map
*/
function bMap(options) {
//object defaults
var defaults = {
mapCenter: [51,0],
mapZoom: 1,
mapCanvas: "map",
mapSidebar: "none",
mapLayerbar: "none",
mapType: google.maps.MapTypeId.ROADMAP,
loadMsg: "<h2>Loading...</h2>"
};
//overide with options
var options = $.extend(defaults, options);
//sidebar control
this.mapSidebar = options.mapSidebar;
this.useSidebar = (this.mapSidebar != "none") ? true : false;
//layerbar control
this.mapLayerbar = options.mapLayerbar;
this.useLayerbar = (this.mapLayerbar != "none") ? true : false;
//Layer array of ojects {data:[],name,type}
this.LyrArr = [];
//render map
var mapOptions = {
zoom: options.mapZoom,
center: new google.maps.LatLng(options.mapCenter[0],options.mapCenter[1]),
mapTypeId: options.mapType
};
this.map = new google.maps.Map(document.getElementById(options.mapCanvas),mapOptions);
this.mapCanvas = options.mapCanvas;
//add the loading div
$("#"+this.mapCanvas).append("<div id='"+this.mapCanvas+"bMapLoadMsg' class='bMapLoadMsg'>"+options.loadMsg+"</div>");
//position loading
$("#"+this.mapCanvas+"bMapLoadMsg").css('left', ($("#map").width()/2)-50 );
$("#"+this.mapCanvas+"bMapLoadMsg").css('top', ($("#map").height()/2)-50 );
//get custom icon array
if(options.icons){
this.icons=options.icons;
}
//draw markers from init vars????
if (options.markers){
this.insertMarkers(options.markers);
}
//infowindow object
this.infoWindow = new google.maps.InfoWindow();
//tools
this.geoCoder = new google.maps.Geocoder();
};
/*
Adds a layer of markers to the map
incomingMarkers:
name string visual name for the layer, appears in layerbar
type string describes the type of layer
visible string ("true"/"false") if the layer should start visible
data array array of bMap marker objects, rendered as GMarkers
icon string address of an image to use as the icon, omit for the default red pushpin
*/
bMap.prototype.insertMarkers = function(incomingMarkers){
tmpThis = this;
var newIndex = tmpThis.LyrArr.length;
//function defaults
var markersDefaults = {
name: "Layer"+newIndex,
type: "marker",
visible:"true"
};
//overide with options
var incomingMarkers = $.extend(markersDefaults, incomingMarkers);
tmpThis.LyrArr[newIndex]=incomingMarkers; //build object
tmpThis.LyrArr[newIndex].toggleLayer = function(){
if( this.visible!="false" ){
this.visible="false";
for(i=0, j=this.data.length; i < j; i++){
this.data[i].setMap(null);
tmpThis.infoWindow.close();
}
$('#bMapLyr'+newIndex).addClass('bLyrHide');
$('#'+tmpThis.mapSidebar+' div[rel^="'+newIndex+'"]').slideUp('fast');
return false;
}else{
this.visible="true";
for(i=0, j=this.data.length; i < j; i++){
this.data[i].setMap(tmpThis.map);
}
$('#bMapLyr'+newIndex).removeClass('bLyrHide');
$('#'+tmpThis.mapSidebar+' div[rel^="'+newIndex+'"]').slideDown('fast');
return true;
}
}
//build pointer array
jQuery.each(incomingMarkers.data, function(i,val) {
//LyrArr[refID].push( new GLatLng(val.lat, val.lng) );
var point = new google.maps.LatLng(val.lat, val.lng);
//create point on map, possible with custom icon
if (val.icon){
tmpThis.LyrArr[newIndex].data[i] = new google.maps.Marker({position:point,map:tmpThis.map,icon:val.icon});
}else{
tmpThis.LyrArr[newIndex].data[i] = new google.maps.Marker({position:point,map:tmpThis.map});
}
//if supplied with marker text, create popup...
if(val.title){
var html = "<h2>"+val.title+"</h2>";
if(val.body){html+=val.body}
google.maps.event.addListener(tmpThis.LyrArr[newIndex].data[i], "click", function(){
tmpThis.infoWindow.setContent(html);
tmpThis.infoWindow.open(tmpThis.map, tmpThis.LyrArr[newIndex].data[i]);
$('#'+tmpThis.mapSidebar+' div').removeClass('bSideSelect');
//highlight the related sidebar div
$('#'+tmpThis.mapSidebar+' div[rel="'+newIndex+' '+i+'"]').addClass('bSideSelect');
//scroll sidebar to item
var x = $('#'+tmpThis.mapSidebar).scrollTop() + $('#'+tmpThis.mapSidebar+' div[rel="'+newIndex+' '+i+'"]').position().top - ( $('#'+tmpThis.mapSidebar).offset().top + ( $('#'+tmpThis.mapSidebar).height()/2 ) );
$('#'+tmpThis.mapSidebar).animate({scrollTop: x}, 500);
});
google.maps.event.addListener(tmpThis.LyrArr[newIndex].data[i], "infowindowclose", function(){
$('#'+tmpThis.mapSidebar+' div[rel="'+newIndex+' '+i+'"]').removeClass('bSideSelect');
});
}
//tmpThis.map.addOverlay( tmpThis.LyrArr[newIndex].data[i] );
if(tmpThis.useSidebar){
$('<div rel="'+newIndex+' '+i+'">' + val.title + '</div>').click(function(){
google.maps.event.trigger(tmpThis.LyrArr[newIndex].data[i], 'click');
//the following line is duplicated in the marker click, but might not always fire there...
$('#'+tmpThis.mapSidebar+' div').removeClass('bSideSelect');
$(this).addClass('bSideSelect');
}).appendTo("#"+tmpThis.mapSidebar);
}
});
if(incomingMarkers.visible!="true"){
for(i=0, j=tmpThis.LyrArr[newIndex].data.length; i < j; i++){
tmpThis.LyrArr[newIndex].data[i].setMap(null);
$('#'+tmpThis.mapSidebar+' div[rel^="'+newIndex+'"]').hide();
}
$('#bMapLyr'+newIndex).addClass('bLyrHide');
return false;
}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
Adds a layer of markers to the map from an external file (AJAX post)
pointsOptions:
serviceURL string the target page for the AJAX request
action string string posted to the AJAX page
vars array array of strings posted to the target page to aid the developer
*/
bMap.prototype.AJAXMarkers = function(pointsOptions){
//function defaults
var pointsDefaults = {
serviceURL: "mapService.php",
action: "getMarkers",
vars: [],
options:{}
};
//overide with options
var pointsOptions = $.extend(pointsDefaults, pointsOptions);
var tmpThis = this;
$("#"+this.mapCanvas+"bMapLoadMsg").show();
//post an AJAX for the points
$.getJSON(pointsOptions.serviceURL, { action: pointsOptions.action, vars: pointsOptions.vars }, function(json){
pointsOptions.options = $.extend(json, pointsOptions.options);
tmpThis.insertMarkers(pointsOptions.options);
$("#"+tmpThis.mapCanvas+"bMapLoadMsg").hide();
});
//return the object, so that chaining is possible with this function ;)
return this;
};
////////////////////// polyline and polygon /////////////////////
/*
Adds a polyline layer to the map
insertLine:
name string visual name for the layer, appears in layerbar
type string describes the type of layer
visible string ("true"/"false") if the layer should start visible
data array array of latlng used to construct line
color string line color, HTML style strings
weight integer the line thickness
opacity real 0.0 - 1.0 line opacity
*/
bMap.prototype.insertLine = function(incomingLine){
tmpThis = this;
var newIndex = tmpThis.LyrArr.length;
//function defaults
var lineDefaults = {
name: "Layer"+newIndex,
type: "line",
visible:"true",
color: "#00F",
weight: 5,
opacity:1
};
//overide with options
var incomingLine = $.extend(lineDefaults, incomingLine);
tmpThis.LyrArr[newIndex]=incomingLine; //build object
tmpThis.LyrArr[newIndex].toggleLayer = function(){
if( this.visible!="false" ){
this.visible="false";
this.data.setMap(null);
$('#bMapLyr'+newIndex).addClass('bLyrHide');
return false;
}else{
this.visible="true";
this.data.setMap(tmpThis.map);
$('#bMapLyr'+newIndex).removeClass('bLyrHide');
return true;
}
}
//build polyline
var tmpArray=[];
//build LatLng array
jQuery.each(incomingLine.data, function(i,val) {
tmpArray.push( new google.maps.LatLng(val.lat, val.lng) );
});
tmpThis.LyrArr[newIndex].data = new google.maps.Polyline({
path: tmpArray,
strokeColor: tmpThis.LyrArr[newIndex].color,
strokeOpacity: parseFloat(tmpThis.LyrArr[newIndex].opacity),
strokeWeight: parseInt(tmpThis.LyrArr[newIndex].weight)
});
tmpThis.LyrArr[newIndex].data.setMap(tmpThis.map);
if(incomingLine.visible!="true"){tmpThis.LyrArr[newIndex].data.setMap(null);}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
Adds a polyLine layer from an AJAX source
pointsOptions:
serviceURL string the target page for the AJAX request
action string string posted to the AJAX page
vars array array of strings posted to the target page to aid the developer
*/
bMap.prototype.AJAXLine = function(lineOptions){
//function defaults
var lineDefaults = {
serviceURL: "mapService.php",
action: "getLine",
vars: []
};
//overide with options
var lineOptions = $.extend(lineDefaults, lineOptions);
var tmpThis = this;
$("#"+this.mapCanvas+"bMapLoadMsg").show();
//post an AJAX for the points
$.post(lineOptions.serviceURL, { action: lineOptions.action, vars: lineOptions.vars }, function(json){
tmpThis.insertLine(json);
$("#"+tmpThis.mapCanvas+"bMapLoadMsg").hide();
}, "json");
//return the object, so that chaining is possible with this function ;)
return this;
};
/*
Adds a polyline layer to the map
insertPolygon:
name string visual name for the layer, appears in layerbar
type string describes the type of layer
visible string ("true"/"false") if the layer should start visible
data array array of latlng used to construct polygon edges
color string polygon color, HTML style strings
weight integer the edge line thickness
opacity real 0.0 - 1.0 interior opacity
*/
bMap.prototype.insertPolygon = function(incomingPolygon){
tmpThis = this;
var newIndex = tmpThis.LyrArr.length;
//function defaults
var polygonDefaults = {
name: "Layer"+newIndex,
type: "polygon",
visible:"true",
color: "#00F",
weight: 5,
opacity:0.5
};
//overide with options
var incomingPolygon = $.extend(polygonDefaults, incomingPolygon);
tmpThis.LyrArr[newIndex]=incomingPolygon; //build object
tmpThis.LyrArr[newIndex].toggleLayer = function(){
if( this.visible!="false" ){
this.visible="false";
this.data.setMap(null);
$('#bMapLyr'+newIndex).addClass('bLyrHide');
return false;
}else{
this.visible="true";
this.data.setMap(tmpThis.map);
$('#bMapLyr'+newIndex).removeClass('bLyrHide');
return true;
}
}
//build polygon
var tmpArray=[];
//build LatLng array
jQuery.each(incomingPolygon.data, function(i,val) {
tmpArray.push( new google.maps.LatLng(val.lat, val.lng) );
});
tmpThis.LyrArr[newIndex].data = new google.maps.Polygon({
path: tmpArray,
strokeColor: tmpThis.LyrArr[newIndex].color,
strokeOpacity:1,
strokeWeight: parseInt(tmpThis.LyrArr[newIndex].weight),
fillColor: tmpThis.LyrArr[newIndex].color,
fillOpacity: parseFloat(tmpThis.LyrArr[newIndex].opacity)
});
tmpThis.LyrArr[newIndex].data.setMap(tmpThis.map);
if(incomingPolygon.visible!="true"){tmpThis.LyrArr[newIndex].data.setMap(null);}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
Adds a polygon layer from an AJAX source
pointsOptions:
serviceURL string the target page for the AJAX request
action string string posted to the AJAX page
vars array array of strings posted to the target page to aid the developer
*/
bMap.prototype.AJAXPolygon = function(polygonOptions){
//function defaults
var polygonDefaults = {
serviceURL: "mapService.php",
action: "getPolygon",
vars: []
};
//overide with options
var polygonOptions = $.extend(polygonDefaults, polygonOptions);
var tmpThis = this;
$("#"+this.mapCanvas+"bMapLoadMsg").show();
//post an AJAX for the points
$.post(polygonOptions.serviceURL, { action: polygonOptions.action, vars: polygonOptions.vars }, function(json){
tmpThis.insertPolygon(json);
$("#"+tmpThis.mapCanvas+"bMapLoadMsg").hide();
}, "json");
//return the object, so that chaining is possible with this function ;)
return this;
};
/*
removes all the layers fromt he map, and tidys the arrays ect
*/
bMap.prototype.removeAllLayers = function(){
//loop all sets of markers, and then loop all markers, and remove everything
for(i=0, j=this.LyrArr.length; i < j; i++){
if (this.LyrArr[i].type=="marker"){
for(i2=0, j2=this.LyrArr[i].data.length; i2 < j2; i2++){
this.LyrArr[i].data[i2].setMap(null);
}
this.infoWindow.close();
} else {
this.LyrArr[i].data.setMap(null);
}
this.LyrArr[i].data = 0;
if(this.useSidebar){
$('#'+this.mapSidebar+' div[rel^="'+i+'"]').remove();
}
}
this.LyrArr.length = 0;
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
remove a single layer from the map (and tidy)
i integer the array index of the laer to remove
*/
bMap.prototype.removeLayer = function(i){
//loop all markers, and remove everything
if (this.LyrArr[i].type=="marker"){
for(i2=0, j2=this.LyrArr[i].data.length; i2 < j2; i2++){
this.LyrArr[i].data[i2].setMap(null);
}
} else {
this.LyrArr[i].data.setMap(null);
}
this.LyrArr[i].data = 0;
if(this.useSidebar){
$('#'+this.mapSidebar+' div[rel^="'+i+'"]').remove();
}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
removes the last item in the layer array
*/
bMap.prototype.popLayer = function(){
var i = this.LyrArr.length - 1; //the actual index is one less than length
var tmpArray = this.LyrArr.pop();
if (tmpArray.type=="marker"){
for(i2=0, j2=tmpArray.data.length; i2 < j2; i2++){
tmpArray.data[i2].setMap(null);
}
} else {
tmpArray.data.setMap(null);
}
tmpArray.data = 0;
if(this.useSidebar){
$('#'+this.mapSidebar+' div[rel^="'+i+'"]').remove();
}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
Removes the first ACTIVE item from the layers array (remaining items DO NOT get shifted afterwards)
*/
bMap.prototype.shiftLayer = function(){
for(i3=0, j3=this.LyrArr.length; i3 < j3; i3++){
if(this.LyrArr[i3].data != 0){
var i = i3;
break;
}
}
if (this.LyrArr[i].type=="marker"){
for(i2=0, j2=this.LyrArr[i].data.length; i2 < j2; i2++){
this.LyrArr[i].data[i2].setMap(null);
}
} else {
this.LyrArr[i].data.setMap(null);
}
//cleanup
this.LyrArr[i].data = 0;
this.LyrArr[i].name="";this.LyrArr[i].type="";
if(this.useSidebar){
$('#'+this.mapSidebar+' ^"'+i+'"]').remove();
}
this.refreshLayerbar();
//return the object, so that chaining is possible with this function ;)
return this;
}
/*
After changes to the layers, this updates the sidebar
*/
bMap.prototype.refreshLayerbar = function(){
if(this.mapLayerbar){
var tmpThis = this;
$("#"+this.mapLayerbar).html('');
for(var i=0, j=this.LyrArr.length; i < j; i++){
if(this.LyrArr[i].data != 0){
//if(this.LyrArr[i].visible!="true"){tmpStr="class='bLyrHide' "}
if( this.LyrArr[i].visible!="false" ){
var tmpStr ="";
}else{
tmpStr ="class='bLyrHide' ";
}
$("<div "+tmpStr+"id='bMapLyr"+i+"' rel='"+i+"'>"+this.LyrArr[i].name+"</div>").click(function(){
//using the objects toggle
tmpThis.LyrArr[ $(this).attr("rel") ].toggleLayer();
}).appendTo("#"+this.mapLayerbar);
}
}
}
}
/*
A wrapper for the client geocoder that takes an address and centers the map at that location
addr string the target address/post code/zip code/location
*/
bMap.prototype.centerAtAddress = function(addr){
var tmpThis = this;
this.geoCoder.geocode({'address': addr}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
tmpThis.map.setCenter(results[0].geometry.location);
}
});
//return the object, so that chaining is possible with this function ;)
return this;
}