-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathui-map.js
289 lines (256 loc) · 12.7 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
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
'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.AMap.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 = 'complete click dblclick mapmove movestart moveend zoomchange zoomstart zoomend mousemove mousewheel mouseover mouseout mouseup mousedown rightclick dragstart dragging dragend resize touchstart touchmove touchend';
var options = uiMapConfig || {};
return {
restrict: 'A',
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 == "gaode" && !map) {
initMap();
}
});
if($window.AMap) {
initMap();
}
function initMap() {
if (opts.uiMapCache && window[attrs.uiMapCache]) {
elm.replaceWith(window[attrs.uiMapCache]);
map = window[attrs.uiMapCache+"Map"];
} else {
map = new window.AMap.Map(elm[0], opts);
/**************** add AMap plugins *******************************/
if (opts.toolbar) {
map.plugin(["AMap.ToolBar"], function () {
var toolBar = new AMap.ToolBar({
ruler: opts.ruler
});
map.addControl(toolBar);
});
}
if (opts.maptype == "SATELLITE") {
map.plugin(["AMap.MapType"], function () {
//地图类型切换
/* MaptypeOptions 类型 说明
defaultType Number 初始化默认图层类型。 取值为0:2D地图 取值为1:卫星图 默认值:0
showTraffic Boolean 叠加实时交通图层 默认值:false
showRoad Boolean 叠加路网图层 默认值:false
*/
var type = new AMap.MapType({
defaultType: 1,
showRoad: true
});
map.addControl(type);
});
} else if (opts.maptype) {
map.plugin(["AMap.MapType"], function () {
var type = new AMap.MapType({
defaultType: 0,
showRoad: true
});
map.addControl(type);
});
}
if (opts.overview) {
map.plugin(["AMap.OverView"], function () {
//加载鹰眼
var view = new AMap.OverView({
isOpen: opts.overview.isOpen || false
});
map.addControl(view);
});
}
if (opts.uiMapCache) {
$window[attrs.uiMapCache+"Map"] = map;
scope.$on("$destroy", function () {
$window[attrs.uiMapCache] = elm;
});
}
/*********************** end add AMap plugins ****************/
}
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 = 'change open close';
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 == "gaode" && !infoWindow) {
initInfoWindow();
}
});
if($window.AMap) {
initInfoWindow();
}
function initInfoWindow() {
if (!infoWindow) {
infoWindow = new window.AMap.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', 'click dblclick rightclick mousemove mouseover mouseout mousedown mouseup dragstart dragging dragend moving moveend movealong touchstart touchmove touchend');
mapOverlayDirective('uiMapPolyline', 'click dblclick rightclick hide show mousedown mouseup mouseover mouseout change touchstart touchmove touchend');
mapOverlayDirective('uiMapPolygon', 'click dblclick rightclick hide show mousedown mouseup mouseover mouseout change touchstart touchmove touchend');
//mapOverlayDirective('uiMapRectangle', 'bounds_changed click dblclick mousedown mousemove mouseout mouseover ' + 'mouseup rightclick');
mapOverlayDirective('uiMapCircle', 'click dblclick rightclick hide show mousedown mouseup mouseover mouseout change touchstart touchmove touchend');
mapOverlayDirective('uiMapGroundImage', 'click dblclick');
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.mapgaodeLoadedCallback = function mapgaodeLoadedCallback(){
scope.$broadcast("map.loaded", "gaode");
};
var params = angular.extend({}, uiMapLoadParams, scope.$eval(attrs.uiMapAsyncLoad));
params.callback = "mapgaodeLoadedCallback";
if(!$window.AMap) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://webapi.amap.com/maps?" + param(params);
document.body.appendChild(script);
}else {
mapgaodeLoadedCallback();
}
}
}
}]);
/**
* 序列化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;
}());