Finding a coordinate in a circle

后端 未结 7 1173
梦谈多话
梦谈多话 2021-01-06 20:51

I am doing a mashup using Google Maps under Grails where users can create geofences by selecting a point on the map and a radius. This get stored on my database and the appl

相关标签:
7条回答
  • 2021-01-06 21:01

    Sadly, most of the responses here won't work for you conveniently, because GPS coordinates are in units of degrees. You will need something to convert from two points in Degrees of latitude and longitude to a great circle distance, which simple Pythagorean theorem falls short of.

    If you're using Google maps API, you can probably do everything you need using GLatLng. As other posters have noted, You can determine the distance between two points is less than the radius of the specified circle. Specifically GLatLng.distance(other:GLatLng) returns the meters distance between too GPS locations.

    To actually display the circles requires a bit more finesse. You will need to create a GPolygon to draw the circumference of the circle. You can find a number of free JavaScript functions that can do this for you.

    0 讨论(0)
  • 2021-01-06 21:09

    if distance from point to center of circle is <= radius of circle then it is inside the circle.
    if the area is made of more than one circle than compare to all the circles... it won't take that long.

    java.awt.geom.Point2D.Double is perfect for this.

    0 讨论(0)
  • 2021-01-06 21:09

    Victor and Beska have the correct answer. That is, if the distance between the point and the center is less than the radius, then it's in the circle.

    For the great circle distance between two points, you can use GeoTools' GeodeticCalculator. In particular you set the point and radius using setStartingGeographicPoint and setDestinationGeographicPoint followed by calling getOrthodromicDistance which will return the distance.

    0 讨论(0)
  • 2021-01-06 21:12

    Since you are using Google Maps and for geographical distances spherical geometry holds rather than euclidean geometry. However if it is relativley smaller distance like a parking lot etc. then you can use euclidean distance formula (http://en.wikipedia.org/wiki/Distance) to find out whether the point is inside or outside the circle.

    I presume you know the coordinates of the circle's center C(xc, yc) and its radius, R. Then for a given point P(x1, y1) find the euclidean distance, D as

    square-root((x1-xc)^2 + (y1-yc)^2)). If D > R, the point lies outside the circle. If D < R, the point lies inside the circle. If D = R, the point lies on the circumference of the circle.

    In case you are doing your measurements over larger distances then you should rather look for Geodesics (please check this http://en.wikipedia.org/wiki/Great-circle_distance).

    I hope it helps.

    cheers

    0 讨论(0)
  • 2021-01-06 21:16

    You want to find the vector that is the distance between the selected coordinate and the center of the circle, then compute the square distance between the selected coordinate and the center of the circle by squaring the components of the vector and adding them together; if that scalar (the squared distance) is less than the square of the radius, the point is within the circle.

    This method avoids having to take a square root, and is just as accurate as normal distance comparison.

    0 讨论(0)
  • 2021-01-06 21:19

    One possibility is to calculate the distance from the centerpoint and compare it to the radius.

    Depending on you application you may be have to take into account that the world is a sphere and not 2Dimensional. To calcualte a distance on earth you can use this formula.

    0 讨论(0)
提交回复
热议问题