-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbingmap.js
230 lines (208 loc) · 8.47 KB
/
bingmap.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
/**
*
* Bing Map for jQuery
*
* @author Aurélien Garroux for Procheo
* @link https://github.com/dhoko/bingmap
* @version Version 2
* @licence BSD
*
*/
/*
Merci à Mathieu Robin (http://www.mathieurobin.com/)
pour son aide et ses conseils.
Un projet issu de mon travail pour Procheo (http://procheo.fr)
*/
(function($){
// Defaults option. lat,lng = Paris
var defaults = {
api : "Your API Key",
type : "road",
zoom : 5,
width : null,
height : null,
latitude : 48.833,
longitude : 2.333,
bing_logo : true,
search_logo : false,
scaleBar : false,
control : false,
copy : false,
disable_zoom : true,
disable_navigate : true
},
// These are to stock, options, the map, a pin and all the pin/map (array)
opt, map, pin, pins = [],
// Bing map can display 3 types of map
typeMap = [{name: "road",def: Microsoft.Maps.MapTypeId.road},{name: "sky",def: Microsoft.Maps.MapTypeId.aerial},{name: "bird",def: Microsoft.Maps.MapTypeId.birdseye}],
type_show = Microsoft.Maps.MapTypeId.road,
methods = {
/**
* Initialize the map, it's the default called method if no-one is given
*
* @param o Array which can contains this options : api,type, zoom, width,copy, height,disable_zoom,disable_navigate bing_logo, search_logo,scaleBar,control latitude, longitude, zoom, markers, popup, cloudmadeAttribution, click event
* @return jQuery Object containing the DOM element extended
*/
init : function(o){
var that = this;
pins[that.attr('id')] = [];
return this.each(function () {
/*if(o) opt = $.extend(defaults, o);*/
if(o) opt = $.extend(true, {}, defaults, o);
for(var i=0; i<=2; i++){
if(opt.type == typeMap[i].name){
type_show = typeMap[i].def;
}
}
map = new Microsoft.Maps.Map(this, {
credentials : opt.api,
mapTypeId : type_show,
center : new Microsoft.Maps.Location(opt.latitude, opt.longitude),
zoom : parseFloat(opt.zoom),
width : that.innerWidth(),
height : that.innerHeight(),
showScalebar : opt.scaleBar,
showMapTypeSelector : false,
showDashboard : opt.control,
showLogo : opt.bing_logo,
showCopyright : opt.copy,
enableSearchLogo : opt.search_logo,
enableClickableLogo : false,
disableKeyboardInput : true,
fixedMapPosition : true,
disableZooming : opt.disable_zoom,
disablePanning : opt.disable_navigate,
});
});
}, //end init
/**
* Put one or more pins on the map
*
* @param o Array or Object which can contains this options : latitude, longitude, drag
* @return jQuery Object containing the DOM element extended
*/
newPin : function(o){
var that = this;
return that.each(function () {
var pin = null;
if("undefined" !== typeof o) {
if("undefined" === typeof o.length) {
var drag = (o.drag) ? true : false;
pin = new Microsoft.Maps.Pushpin({
"latitude" : o.latitude,
"longitude": o.longitude},{
draggable: drag
});
map.entities.push(pin);
pins[that.attr('id')].push(pin);
}else{
for(pin in o) {
that.bingmap('newPin', o[pin]);
}
}
}
if("undefined" !== typeof o.popup){
o.popup.geo = {"latitude" : o.latitude, "longitude": o.longitude};
o.popup.pin = pin;
that.bingmap('addPopup',o.popup);
}//end popu
});
}, //end newPin
/**
* Put a popup on the map (DO NOT USE THIS METHOD)
* It works, but it will be a private method (v2.1)
*
* @param o Object which can contains this options : title, description, width, height, visible, showPointer, offset
* @return jQuery Object containing the DOM element extended
* TODO sortir la methode et retoucher les XInfoBox()
*/
addPopup : function(o){
if("undefined" == typeof o.html) o.html = "";
if("undefined" == typeof o.width) o.width = 200;
if("undefined" == typeof o.height) o.height = 50;
var visible = true;
if("undefined" !== typeof o.onShow) visible = false;
var infoboxOptions = {
title : o.title,
description : o.html,
width : o.width,
height : o.height,
visible : visible,
showPointer : false,
offset : new Microsoft.Maps.Point(15,35)
};
var infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(o.geo.latitude, o.geo.longitude),infoboxOptions);
if("undefined" !== typeof o.onShow){
Microsoft.Maps.Events.addHandler(o.pin, 'mouseover', showInfobox);
Microsoft.Maps.Events.addHandler(o.pin, 'mouseout', hideInfobox);
function showInfobox(){infobox.setOptions({visible:true});}
function hideInfobox() {infobox.setOptions({visible:false});}
}
map.entities.push(infobox);
}, //end addPopup
/**
* Callback method to use API
*
* @param o function use lambda function
* @return function width {pins[map]},{map}
*/
onPin : function(o) {
return this.each(function (n,m) {
return o(pins[m.getAttribute('id')],map);
});
},
/**
* Callback method to use API
*
* @param o function use lambda function
* @return function width {map}
*/
onMap : function(o) {
return o(map);
},
/**
* Callback method to get all pins by selected map
*
* @return Array pins
*/
getPin : function(){
return pins[this.attr('id')];
},
/**
* Callback method to get the map
*
* @return jQuery Object map
*/
getMap : function(){
return map;
},
/**
* Callback method using the Rest API
* Calls the API width longitude & latitude to get info about this point (Address)
*
* @param o Object longitude,latitude,callback(function(status, data))
* @return function width {status, data}
*/
getInfo : function(o){
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/"+o.latitude+","+o.longitude+"?o=json&jsonp=?&key=" + opt.api;
return this.each(function (n,m) {
$.getJSON(geocodeRequest,function(data){
var status = (data.authenticationResultCode == "ValidCredentials") ? "success" : "fail";
return o.callback(status,data.resourceSets);
});
});
}
};
/**
* Bootstrap method, must be not modified
*/
$.fn.bingmap = function (method) {
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.BingMap');
}
};
})(jQuery);