-1

I want to know is there any java api available for google map to check particular longitude and latitude within circle.I am not using any android or ios client.I need to do this on backend server. I found there is a way to resolve this issue in android sdk [Check if a latitude and longitude is within a circle ] But I need some reference how to resolve this issue in backend server.

These are the Requirement I want

User Send Location : lat1,lng1

Server circle location : latCenter,lngCenter,radius

What I need was check the user send location inside the google map circle. In google I found this reference [https://github.com/googlemaps/google-maps-services-java] But this is not give any solution to my problem. I want to know Is there any standard api available for google maps or I need to create function to validate the location.

Sajith Vijesekara
  • 1,324
  • 2
  • 17
  • 52

1 Answers1

1

There are multiple options for this. One is a bit more complicated and involves the fact that a circle is uniquely defined by 3 points on its boundary and a computation of the determinant of a suitable matrix (including coordinates of the three points plus your point of reference). While this is an elaborate approach, you can get off a lot easier:

The disc surrounded by the circle (with radius r and center c_x, c_y) is given by the equation

{(p,q) | (p-c_x)^2 + (q-c_y)^2 < r^2}

(replace the less-than by less-or-equal-than if you count the boundary as inside too).

That means you can just compute the value

(p-c_x)^2 + (q-c_y)^2

and compare it with r^2 to get the information if the point (p,q) is inside, outside or on the circle.

Note that due to floating point inaccuracy in computer systems and depending on your use case you might not want to check for exact equality for the boundary but check if the difference of both values is close to 0.

edit: formatting

Jay Schneider
  • 325
  • 1
  • 7
  • 1
    If full presicion is required dont't use float types at all but rather `BigDecimal`. – LuCio Jul 04 '18 at 06:58
  • Hi @Jay Thanks for your response.It means there is no standard API supported by google maps for this problem. – Sajith Vijesekara Jul 04 '18 at 07:08
  • @LuCio Thanks for your Reply – Sajith Vijesekara Jul 04 '18 at 07:09
  • @SajithVijesekara No I didn't want to claim that google maps does not have a predefined function for it (as I don't really know the API). I just wanted to state that the computation is easy (and probably fast) so if you don't find a function for it you can easily implement it like this yourself. Cheers – Jay Schneider Jul 04 '18 at 07:13