I am using a special Map SDK for iOS and I am adding a custom shape to the map. The shape is always a different size and it could be a circle, square, star etc. the point being it is always dynamic whenever the app is run.
After adding this shape to the map, I can access it's property called overlayBounds
which is described as: This property contains the smallest rectangle that completely encompasses the overlay.
The overlay
is my shape that I'm adding to the map.
Whenever a location update is generated by CLLocationManager
, I want to check and see if the most recent coordinate is inside of that overlayBounds
property of the shape.
When accessing overlayBounds
, it has an ne
property and a sw
property. Both of these are just CLLocationCoordinate2D's
So, if the overlayBounds
is made up of two CLLocationCoordinate2D's
and the CLLocationManager
is always updating the user's location and giving me the most recent coordinate(CLLocationCoordinate2D
), how can I check if that most recent coordinate is within the overlayBounds
?
After doing a lot of research I have only found one potential solution to go off of which is this: https://stackoverflow.com/a/30434618/3344977
But that answer assumes that my overlayBounds
property has 4 coordinates(CLLocationCoordinate2D's
), when I only have 2.
Your description seems much harder then the actual question. So if I am getting this correctly your question is only to check if the point is inside the rectangle described in overlayBounds
.
You have only 2 points as it is enough to define a rectangle. So NE
and SW
are the two points where the other two are received as (NE.x, SE.y)
and (SE.x, NE.y)
. With this you may use the answer you linked or you may simply construct a MKMapRect
where origin is NE
and size is SE-NE
. So in this case you may simply use MKMapRectMake
and then use MKMapRectContainsPoint
. BUT watch out when computing size as SE-NE
might produce negative results in which cases you need to add degrees to the size. That is 180 to x
(latitude) and 360 to y
(longitude)...
MKMapRect rect = MKMapRectMake(NE.latitude, NE.longitude, SE.latitude-NE.latitude, SE.longitude-NE.longitude);
if(rect.width < .0) rect.width += 180.0;
if(rect.height < .0) rect.height += 360.0;
BOOL pointInside = MKMapRectContainsPoint(rect, pointOnMap);
Something like this should do the trick.
Now if you are trying to check if the point is inside the shape itself it really depends on how your shape is defined. If this is some form of analytic representation you might find some method already made for you to return the value but if not then your best shot would most likely be drawing the shape to some canvas and checking the color of canvas at the location you need to check. In any case the bigger problem here is converting the point and the rect to a Cartesian coordinate system. If that is the case then just add a comment and I will try to help you on that...