2

I have some KML data which defines an area on a map, such as the following:

131.0764742247829,-15.80786022929951,0 132.6357700620065,-16.54895727734661,0
134.1119108999971,-17.28040233069844,0 135.8545033459996,-18.1298170074137,0 
137.7396886168202,-19.07860187595016,0 140.011948401144,-20.18462043802856,0 
142.3114600417346,-21.19369381963822,0 144.1566045495202,-22.15731684591415,0

A map with the KML-defined region overlaid on it

I'd like to determine within my iOS application if the user's current location is inside of this defined area. How can I do this?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Desmond
  • 5,001
  • 14
  • 56
  • 115
  • possible duplicate of [How to determine if an annotation is inside of MKPolygonView (iOS)](http://stackoverflow.com/questions/4354130/how-to-determine-if-an-annotation-is-inside-of-mkpolygonview-ios) – Jano Oct 18 '11 at 11:12
  • Hi jano, thanks for the comment, i'm actually wanted to know if i'm inside a kml location range – Desmond Oct 19 '11 at 02:39
  • So you want to know if you are within a certain distance of any one of these points? – NJones Oct 19 '11 at 03:52
  • not necessary, i just need to determine if i'm in the KML, inside the blue line. sorry starting to learn mapping in iOS, pardon me if i used the wrong words. i wanna know if my location blue dot are inside this particular KML.... i have multiple KML to check against with. infact 27 of them. – Desmond Oct 19 '11 at 03:59
  • 1
    I think you dismissed Jano's link to quickly. The real question, and I don't know the answer to this one, is: How do you convert a KML location range to a CGPath or MKPolygonView? Sorry I don't know the answer. I hope that points you in right direction. – NJones Oct 19 '11 at 04:17
  • I missed sosborn's update, sorry, I would try that first. – NJones Oct 19 '11 at 04:23
  • Thanks for the reply NJones. will try it out, i myself also don't know if it workable, just started iOS programming couple months ago. learning mapping now. – Desmond Oct 19 '11 at 04:28

1 Answers1

6

If you know the center and radius of the circle then it is quite easy.

CLRegion *circle = [CLRegion initCircularRegionWithCenter:centerCoordinate radius:circleRadius identifier:@"myCircle"];

BOOL doesItContainMyPoint = [circle containsCoordinate:myLocation];

Update based on revised question

I've never tried this, but couldn't you create a UIBezierPath with your points (you don't have to actually draw the bezier path) and then use UIBezierPath's - (BOOL)containsPoint:(CGPoint)point to test for inclusion?

sosborn
  • 14,676
  • 2
  • 42
  • 46
  • hi sosborn, thanks for the comment, the circle are jus an example. just updated my question with pictures – Desmond Oct 19 '11 at 03:41
  • thanks for the updated answer sosborn, will you be able to show me a short tutorial on how it works ? as i'm pretty new in iOS mapping, how do i parse KML into iPhone ? – Desmond Oct 19 '11 at 04:30
  • Well, your coordinates are no different then coordinates for graphics, that is, they represent an x and a y. So, you can create a UIBezierPath and segments to it using the addLineToPoint: method. As I said, I've never done it, but just look at the UIBezierPath documentation and make the mental shift from a GPS coordinate to a graphic point. – sosborn Oct 19 '11 at 04:35
  • thanks for the reply, will learn how to do it, i testing out simple KML to parse the value – Desmond Oct 19 '11 at 06:21