Created
November 24, 2011 16:08
-
-
Save goganchic/1391699 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Circle = (center, radius, options) -> | |
options = $.extend({accuracy : 32}, options) | |
# Вызывает родительский конструктор | |
YMaps.Polygon.call(@, [], options) | |
# Вызывается при добавлении круга на карту | |
@.onAddToMap = (map, container) -> | |
YMaps.Polygon.prototype.onAddToMap.call(@, map, container) | |
@.updatePoints() | |
# Устанавливает новый центр и радиус | |
@.setCenter = (newCenter, newRadius) -> | |
if @.getMap() && (!center.equals(newCenter) || radius != newRadius) | |
center = newCenter | |
radius = newRadius || radius | |
@.updatePoints() | |
# Вычисляет точки окружности | |
@.updatePoints = -> | |
map = @.getMap() | |
# Откладываем геоточку от центра к северу на заданном расстоянии | |
northPoint = new YMaps.GeoPoint(center.getLng(), center.getLat() + radius / 112.2) | |
# Пиксельные координаты на последнем масштабе | |
pixCenter = map.coordSystem.fromCoordPoint(center) | |
# Радиус круга в пикселях | |
pixRadius = pixCenter.getY() - map.coordSystem.fromCoordPoint(northPoint).getY() | |
# Вершины многоугол | |
points = []; | |
# Вспомогательные переменные | |
twoPI = 2 * Math.PI | |
delta = twoPI / options.accuracy | |
for alpha in [0 .. twoPI] by delta | |
points.push( | |
map.coordSystem.toCoordPoint( | |
new YMaps.Point( | |
Math.cos(alpha) * pixRadius + pixCenter.getX(), | |
Math.sin(alpha) * pixRadius + pixCenter.getY() | |
) | |
) | |
) | |
@.setPoints points | |
console.log 'test' | |
YMaps.Utils.extend Circle, YMaps.Polygon |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment