-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui-map.js
254 lines (218 loc) · 9.31 KB
/
ui-map.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
'use strict';
(function () {
var app = angular.module('ui.map', ['ui.event']);
//Setup map events from a google map object to trigger on a given element too,
//then we just use ui-event to catch events from an element
function bindMapEvents(scope, eventsStr, googleObject, element) {
angular.forEach(eventsStr.split(' '), function (eventName) {
//Prefix all googlemap events with 'map-', so eg 'click'
//for the googlemap doesn't interfere with a normal 'click' event
window.qq.maps.event.addListener(googleObject, eventName, function (event) {
element.triggerHandler('map-' + eventName, event);
//We create an $apply if it isn't happening. we need better support for this
//We don't want to use timeout because tons of these events fire at once,
//and we only need one $apply
if (!scope.$$phase) {
scope.$apply();
}
});
});
}
app.value('uiMapConfig', {})
.directive('uiMap', ['uiMapConfig', '$window', '$parse',
function (uiMapConfig, $window, $parse) {
var mapEvents = 'click dblclick rightclick mouseover mouseout mousemove drag dragstart dragend bounds_changed center_changed zoom_changed maptypeid_changed projection_changed idle tilesloaded resize';
var options = uiMapConfig || {};
return {
restrict: 'A',
controller: function ($scope, $element) {
},
link: function (scope, elm, attrs) {
var map;
var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions));
scope.$on("map.loaded", function (e, type) {
if (type == "qq" && !map) {
initMap();
}
});
if ($window.qq && $window.qq.maps && $window.qq.maps.Map) {
initMap();
}
function initMap() {
if (opts.uiMapCache && $window[attrs.uiMapCache]) {
elm.replaceWith(window[attrs.uiMapCache]);
map = $window[attrs.uiMapCache + "Map"];
} else {
if (opts.ngCenter &&
angular.isNumber(opts.ngCenter.lat) &&
angular.isNumber(opts.ngCenter.lng)) {
opts.center = new qq.maps.LatLng(opts.ngCenter.lat, opts.ngCenter.lng);
}
if (angular.isNumber(opts.ngZoom)) {
opts.zoom = opts.ngZoom;
}
map = new qq.maps.Map(elm[0], opts);
}
var model = $parse(attrs.uiMap);
//Set scope variable for the map
model.assign(scope, map);
bindMapEvents(scope, mapEvents, map, elm);
}
}
};
}
]);
app.value('uiMapInfoWindowConfig', {})
.directive('uiMapInfoWindow', [
'uiMapInfoWindowConfig', '$window', '$parse', '$compile',
function (uiMapInfoWindowConfig, $window, $parse, $compile) {
var infoWindowEvents = 'content_changed position_changed zindex_changed closeclick closeclick';
var options = uiMapInfoWindowConfig || {};
return {
link: function (scope, elm, attrs) {
var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions));
opts.content = elm[0];
var model = $parse(attrs.uiMapInfoWindow);
var infoWindow = model(scope);
scope.$on("map.loaded", function (e, type) {
if (type == "qq" && !infoWindow) {
initInfoWindow();
}
});
if ($window.qq && $window.qq.maps && $window.qq.maps.Map) {
initInfoWindow();
}
function initInfoWindow() {
if (!infoWindow) {
infoWindow = new window.qq.maps.InfoWindow(opts);
model.assign(scope, infoWindow);
bindMapEvents(scope, infoWindowEvents, infoWindow, elm);
/* The info window's contents dont' need to be on the dom anymore,
google maps has them stored. So we just replace the infowindow element
with an empty div. (we don't just straight remove it from the dom because
straight removing things from the dom can mess up angular) */
elm.replaceWith('<div></div>');
//Decorate infoWindow.open to $compile contents before opening
var _open = infoWindow.open;
infoWindow.open = function open(a1, a2, a3, a4, a5, a6) {
$compile(elm.contents())(scope);
_open.call(infoWindow, a1, a2, a3, a4, a5, a6);
};
}
}
}
};
}
]);
/*
* Map overlay directives all work the same. Take map marker for example
* <ui-map-marker="myMarker"> will $watch 'myMarker' and each time it changes,
* it will hook up myMarker's events to the directive dom element. Then
* ui-event will be able to catch all of myMarker's events. Super simple.
*/
function mapOverlayDirective(directiveName, events) {
app.directive(directiveName, [function () {
return {
restrict: 'A',
link: function (scope, elm, attrs) {
scope.$watch(attrs[directiveName], function (newObject) {
if (newObject) {
bindMapEvents(scope, events, newObject, elm);
}
});
}
};
}]);
}
mapOverlayDirective('uiMapMarker', 'animation_changed clickable_changed cursor_changed draggable_changed flat_changed icon_changed map_changed position_changed shadow_changed shape_changed title_changed visible_changed zindex_changed click mousedown mouseup mouseover mouseout dblclick rightclick dragstart dragging dragend');
mapOverlayDirective('uiMapPolyline', 'map_changed visible_changed zindex_changed click dblclick rightclick mousedown mouseup mouseover mouseout mousemove');
mapOverlayDirective('uiMapPolygon', 'map_changed visible_changed zindex_changed click dblclick rightclick mousedown mouseup mouseover mouseout mousemove');
mapOverlayDirective('uiMapCircle', 'center_changed map_changed radius_changed visible_changed zindex_changed click dblclick rightclick mousedown mouseup mouseover mouseout mousemove');
mapOverlayDirective('uiMapGroundOverlay', 'map_changed visible_changed click mousedown mousemove mouseout mouseover mouseup rightclick');
app.provider('uiMapLoadParams', function uiMapLoadParams() {
var params = {};
this.setParams = function (ps) {
params = ps;
};
this.$get = function uiMapLoadParamsFactory() {
return params;
};
})
.directive('uiMapAsyncLoad', ['$window', '$parse', 'uiMapLoadParams',
function ($window, $parse, uiMapLoadParams) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
$window.mapqqLoadedCallback = function mapqqLoadedCallback() {
scope.$broadcast("map.loaded", "qq");
};
var params = angular.extend({}, uiMapLoadParams, scope.$eval(attrs.uiMapAsyncLoad));
params.callback = "mapqqLoadedCallback";
if (!($window.qq && $window.qq.maps && $window.qq.maps.Map)) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://map.qq.com/api/js?" + param(params);
document.body.appendChild(script);
} else {
mapqqLoadedCallback();
}
}
}
}]);
/**
* 序列化js对象
*
* @param a
* @param traditional
* @returns {string}
*/
function param(a, traditional) {
var prefix,
s = [],
add = function (key, value) {
// If value is a function, invoke it and return its value
value = angular.isFunction(value) ? value() : ( value == null ? "" : value );
s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
};
// If an array was passed in, assume that it is an array of form elements.
if (angular.isArray(a) || ( a.jquery && !angular.isObject(a) )) {
// Serialize the form elements
angular.forEach(a, function () {
add(this.name, this.value);
});
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for (prefix in a) {
buildParams(prefix, a[prefix], traditional, add);
}
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
}
var r20 = /%20/g;
function buildParams(prefix, obj, traditional, add) {
var name;
if (angular.isArray(obj)) {
// Serialize array item.
angular.forEach(obj, function (v, i) {
if (traditional || rbracket.test(prefix)) {
// Treat each array item as a scalar.
add(prefix, v);
} else {
// Item is non-scalar (array or object), encode its numeric index.
buildParams(prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add);
}
});
} else if (!traditional && angular.isObject(obj)) {
// Serialize object item.
for (name in obj) {
buildParams(prefix + "[" + name + "]", obj[name], traditional, add);
}
} else {
// Serialize scalar item.
add(prefix, obj);
}
}
var decode = decodeURIComponent;
}());