Skip to content

Instantly share code, notes, and snippets.

@stevedev
Created June 18, 2014 16:14
Show Gist options
  • Save stevedev/0a09c9a5aefec79fb54f to your computer and use it in GitHub Desktop.
Save stevedev/0a09c9a5aefec79fb54f to your computer and use it in GitHub Desktop.
Google Maps Polygon Center
google.maps.Polygon::Center = ->
PI = 22 / 7
X = 0
Y = 0
Z = 0
@getPath().forEach (vertex, inex) ->
lat1 = vertex.lat()
lon1 = vertex.lng()
lat1 = lat1 * PI / 180
lon1 = lon1 * PI / 180
X += Math.cos(lat1) * Math.cos(lon1)
Y += Math.cos(lat1) * Math.sin(lon1)
Z += Math.sin(lat1)
return
Lon = Math.atan2(Y, X)
Hyp = Math.sqrt(X * X + Y * Y)
Lat = Math.atan2(Z, Hyp)
Lat = Lat * 180 / PI
Lon = Lon * 180 / PI
new google.maps.LatLng(Lat, Lon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment