Skip to content

Instantly share code, notes, and snippets.

@goganchic
Created November 24, 2011 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goganchic/1391698 to your computer and use it in GitHub Desktop.
Save goganchic/1391698 to your computer and use it in GitHub Desktop.
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