Skip to content

Instantly share code, notes, and snippets.

@tcql
Last active December 21, 2015 21:18
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 tcql/6366841 to your computer and use it in GitHub Desktop.
Save tcql/6366841 to your computer and use it in GitHub Desktop.
first Leaflet layer! Still could use some cleanup and whatnot, but It works pretty well.
class L.PolyCircle extends L.Path
options:
polyOptions: {}
nodes: 16
initialize: (center, radius, options)->
L.setOptions @,options
@setCenter center
@setRadius radius
setCenter: (center)->
@_center = center
@LATCONV = @_getLatConversion()
@LNGCONV = @_getLngConversion()
if @_poly?
@makePoly()
setRadius: (radius)->
@_radius = radius
if @_poly?
@makePoly()
makePoly: ()->
step = parseInt 360/@options.nodes;
points = []
# increments by the value of step on each iteration
for i in [0..360] by step
points.push @_makePoint(i)
if not @_poly?
@_poly = new L.Polygon(points, @options.polyOptions)
else
@_poly.setLatLngs points
return @_poly
getPoly: ()->
if @_poly?
return @_poly
else
return @makePoly()
onAdd: (map)->
poly = @getPoly()
poly.onAdd map
onRemove: (map)->
poly = @getPoly()
poly.onRemove map
toGeoJSON: ()->
poly = @getPoly()
return poly.toGeoJSON()
_makePoint:(angle)->
lat = @_center.lat+(@_radius/@LATCONV * Math.cos(angle * (Math.PI/180)))
lng = @_center.lng+(@_radius/@LNGCONV * Math.sin(angle * (Math.PI/180)))
return new L.LatLng(lat,lng)
_getLatConversion: ()->
return @_center.distanceTo(new L.LatLng(@_center.lat+0.1, @_center.lng))*10
_getLngConversion: ()->
return @_center.distanceTo(new L.LatLng(@_center.lat, @_center.lng+0.1))*10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment