我正在使用CGPathContainsPoint来确定坐标是否在地图上的多边形内,但无论我选择哪个坐标,它总是返回false.
任何人都可以解释究竟出了什么问题?下面是我在Swift中的代码.
class VIEwController: UIVIEwController,MKMapVIEwDelegate@IBOutlet weak var mapVIEw: MKMapVIEw! let initialLocation = CLLocation(latitude: 43.656734,longitude: -79.381576) let point = CGPointMake(43.656734,-79.381576) let regionRadius: CLLocationdistance = 500 let point1 = CLLocationCoordinate2D(latitude: 43.656734,longitude: -79.381576) var points = [CLLocationCoordinate2DMake(43.655782,-79.382094),CLLocationCoordinate2DMake(43.657499,-79.382310),CLLocationCoordinate2DMake(43.656656,-79.380497),CLLocationCoordinate2DMake(43.655782,-79.382094)] overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() centerMapOnLocation(initialLocation) let polygon = MKpolygon(coordinates: &points,count: points.count) mapVIEw.addOverlay(polygon) var annotation = MKPointAnnotation() annotation.coordinate = point1 annotation.Title = "Test" annotation.subTitle = "Test" mapVIEw.addAnnotation(annotation) self.mapVIEw.delegate = self } func centerMapOnLocation(location: CLLocation) { let coordinateRegion = MKCoordinateRegionMakeWithdistance(location.coordinate,regionRadius * 2.0,regionRadius * 2.0) mapVIEw.setRegion(coordinateRegion,animated: true) } func mapVIEw(mapVIEw: MKMapVIEw!,rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if overlay is MKpolygon { let polygonVIEw = mkpolygonrenderer(overlay: overlay) polygonVIEw.strokecolor = UIcolor.redcolor() if CGPathContainsPoint(polygonVIEw.path,nil,CGPointMake(43.656734,-79.381576),true) { print("True!!!!!") } else { println("False") } return polygonVIEw } return nil }解决方法 针对Swift 3进行了更新
let polygonRenderer = mkpolygonrenderer(polygon: polygon)let mapPoint: MKMapPoint = MKMapPointForCoordinate(coordinate)let polygonVIEwPoint: CGPoint = polygonRenderer.point(for: mapPoint)if polygonRenderer.path.contains(polygonVIEwPoint){ print("Your location was insIDe your polygon.")}总结
以上是内存溢出为你收集整理的ios – 确定纬度/经度点是否在Mapview中的MKPolygon中?全部内容,希望文章能够帮你解决ios – 确定纬度/经度点是否在Mapview中的MKPolygon中?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)