Skip to content

Instantly share code, notes, and snippets.

@fraserxu
Created July 1, 2014 08:42
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 fraserxu/998206ca23773e8b2dc0 to your computer and use it in GitHub Desktop.
Save fraserxu/998206ca23773e8b2dc0 to your computer and use it in GitHub Desktop.
A function to calculate if a geo point is in a cirlce
###*
* Check if a given point in a circle
* @param {Object} circle a cirle object with radius and center point
* @param {Object} point a point need to be checked
* @return {Boolean} True or False
###
Map.isInArea = (circle, point) ->
getDistance = (lng1, lat1, lng2, lat2) ->
# Radisu of the erath in km
R = 6371
dLat = (lat2 - lat1).toRad()
dLng = (lng2 - lng1).toRad()
a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * Math.sin(dLng/2) * Math.sin(dLng/2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
d = R * c
return d
# convert numeric degrees to radians
if typeof Number.prototype.toRad is "undefined"
Number.prototype.toRad = ->
return this * Math.PI / 180
# Convert unit
# Unit 1 Km = 0.621371 Mile
K2M = (k) ->
UNIT = 0.621371
return k * UNIT
distance = K2M getDistance(circle.lng, circle.lat, point.lng, point.lat)
return if distance > circle.radius then false else true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment