swift2 - Swift 2 - User's Current Location is inside MKPolygon -
i trying check if user's current location inside mkpolygon. have created following function returns false test cases. there might doing wrong?
func iscorrectregion(coordinates: array<json>, userlocation: cllocationcoordinate2d) -> bool { var newcoordinates: [cllocationcoordinate2d] = [] var mappoints: [mkmappoint] = [] for(a):(json) in coordinates { let lat = a.arrayvalue[1].double let long = a.arrayvalue[0].double let location = cllocationcoordinate2d(latitude: lat!, longitude: long!) newcoordinates.append(location) } b in newcoordinates { let c: mkmappoint = mkmappointforcoordinate(b) mappoints.append(c) } let polygon: mkpolygon = mkpolygon(points: &mappoints, count: mappoints.count) let polyrender: mkpolygonrenderer = mkpolygonrenderer(polygon: polygon) polyrender.invalidatepath() let target: mkmappoint = mkmappointforcoordinate(userlocation) let cgtarget: cgpoint = cgpoint(x: target.x, y: target.y) let iswithin = cgpathcontainspoint(polyrender.path, nil, cgtarget, false) return iswithin }
finally figured out after trying few different things. hope helps others:
func iscorrectregion(coordinates: array<json>, userlocation: cllocationcoordinate2d) -> bool { var newcoordinates: [cllocationcoordinate2d] = [] var mappoints: [mkmappoint] = [] let mpr: cgmutablepathref = cgpathcreatemutable() for(a):(json) in coordinates { let lat = a.arrayvalue[1].double let long = a.arrayvalue[0].double let location = cllocationcoordinate2d(latitude: lat!, longitude: long!) newcoordinates.append(location) } b in newcoordinates { let c: mkmappoint = mkmappointforcoordinate(b) mappoints.append(c) } var p = 0; p<mappoints.count; p++ { if p == 0 { cgpathmovetopoint(mpr, nil, cgfloat(mappoints[p].x), cgfloat(mappoints[p].y)) } else { cgpathaddlinetopoint(mpr, nil, cgfloat(mappoints[p].x), cgfloat(mappoints[p].y)) } } let target: mkmappoint = mkmappointforcoordinate(userlocation) let cgtarget: cgpoint = cgpoint(x: target.x, y: target.y) let iswithin = cgpathcontainspoint(mpr, nil, cgtarget, false) return iswithin }
Comments
Post a Comment