filipehd
filipehd

Reputation: 930

How to draw markers inside a specific radius for google maps in android

I have a Google Map fragment on my android application.

I have markers drawn on the map which represent places of interest and I have an extra functionality that draws a circle on the center with a radius defined by the user.

What I want to do is that only the markers which are contained inside this circle shall be displayed on map. I am using the Circle object, from Google maps Shapes, to draw the circle.

The problem is that, although the center of the circle is defined in Latitude/Longitude coordinates, it's radius is defined in meters. So how can I calculate if a position of a place in LatLng is contained within a radius defined in meters?

Thank you

Upvotes: 0

Views: 3592

Answers (2)

MaciejGórski
MaciejGórski

Reputation: 22232

All you need is to calculate if two LatLngs are closer to each other than radius.

For that you can use Location.distanceBetween.

See the Circle.contains function here for an example.

Upvotes: 3

tyczj
tyczj

Reputation: 73751

Basically what you are going to have to do is test all points and use ray casting to test the points against sides the circle.

If you find only 1 collision with a side then the point is inside the circle, if you find 2 collisions spots then it is outside the circle.

It sounds intimidating but its not too bad once you understand it

Here is excellent documentation on what is involved. I used a combination of the first and second answer to something similar

Upvotes: 2

Related Questions