forked from dimik/ymaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoints-generator.js
142 lines (127 loc) · 5.38 KB
/
points-generator.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
/**
* Random-генератор маркеров.
* @class
* @name RandomPointsGenerator
* @param {Number} count Количество меток которые надо создать.
* @example
var placemarks = RandomPointsGenerator.generate(200)
.atBounds(myMap.getBounds());
*/
function RandomPointsGenerator(count) {
this.count = count || 0;
}
/**
* Статический метод для удобства инстанцирования.
* @static
* @function
* @name RandomPointsGenerator.generate
* @param {Number} count Количество меток которые надо создать.
* @returns {RandomPointsGenerator} Экземпляр генератора маркеров.
*/
RandomPointsGenerator.generate = function (count) {
return new RandomPointsGenerator(count);
};
/**
* Устанавливает количество маркеров для генерации.
* @function
* @name RandomPointsGenerator.generate
* @param {Number} count Количество меток которые надо создать.
* @returns {RandomPointsGenerator} Экземпляр генератора маркеров.
*/
RandomPointsGenerator.prototype.generate = function (count) {
this.count = count;
return this;
};
/**
* Генерит случайным образом маркеры в области bounds.
* @function
* @name RandomPointsGenerator.atBounds
* @param {Number[][]} bounds Область видимости маркеров.
* @returns {ymaps.Placemark[]} Массив маркеров.
*/
RandomPointsGenerator.prototype.atBounds = function (bounds) {
// протяженность области просмотра в градусах
var span = [bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1]],
points = [];
for(var i = 0; i < this.count; i++) {
points[i] = this.createMarker([Math.random() * span[0] + bounds[0][0], Math.random() * span[1] + bounds[0][1]], i);
}
return points;
};
/**
* Генерит случайным образом маркеры внутри окружности с данным центром и радиусом.
* @function
* @name RandomPointsGenerator.atCenterAndRadius
* @param {Number[]} center Координаты центра окружности.
* @param {Number} radius Радиус окружности в метрах.
* @returns {ymaps.Placemark[]} Массив маркеров.
*/
RandomPointsGenerator.prototype.atCenterAndRadius = function (center, radius) {
var coordSystem = ymaps.coordSystem.geo,
distance, direction, coords, points = [];
for(var i = 0; i < this.count; i++) {
direction = [Math.random() - Math.random(), Math.random() - Math.random()];
distance = radius * Math.random();
coords = coordSystem.solveDirectProblem(center, direction, distance).endPoint;
points[i] = this.createMarker(coords, i);
}
return points;
};
/**
* TODO
* Генерит случайным образом маркеры внутри области с данным центром и линейными размерами.
* @function
* @name RandomPointsGenerator.atCenterAndSize
* @param {Number[]} center Координаты центра области.
* @param {Number[]} size Линейные размеры области в метрах.
* @returns {ymaps.Placemark[]} Массив маркеров.
*/
RandomPointsGenerator.prototype.atCenterAndSize = function (center, size) {};
/**
* Создает маркер по координатам.
* @function
* @name RandomPointsGenerator.createMarker
* @param {Number[]} coordinates Массив координат.
* @param {Number} index Индекс маркера.
* @returns {ymaps.Placemark} Метка.
*/
RandomPointsGenerator.prototype.createMarker = function (coordinates, index) {
return new ymaps.GeoObject({
geometry: {
type: "Point",
coordinates: coordinates
},
properties: this.getPointData(index)
}, this.getPointOptions(index));
};
/**
* Метод для перекрытия. Возвращает объект с данными,
* который передается как поле properties в конструктор геообъекта.
* @function
* @name RandomPointsGenerator.getPointData
* @param {Number} index Индекс маркера.
* @returns {Object} Данные метки.
*/
RandomPointsGenerator.prototype.getPointData = function (index) {
return {};
};
/**
* Метод для перекрытия. Возвращает объект с опциями,
* который передается как параметр options в конструктор геообъекта.
* @function
* @name RandomPointsGenerator.getPointOptions
* @param {Number} index Индекс маркера.
* @returns {Object} Опции метки.
* @example
var generator = RandomPointsGenerator.generate(200);
// Перекрываем метод для создания меток со случайным хначением опции preset.
generator.getPointOptions = function (i) {
var presets = ['twirl#blueIcon', 'twirl#orangeIcon', 'twirl#darkblueIcon', 'twirl#pinkIcon'];
return {
preset: presets[Math.floor(Math.random() * presets.length)]
};
};
*/
RandomPointsGenerator.prototype.getPointOptions = function (index) {
return {};
};