MKPolygon - 如何确定 CLLocationCooperative2D 是否位于 CLLocationCooperative2D 多边形中?

2023-11-29

我有下面的快速代码,它绘制一个多边形并在 MKMapView 上添加注释。我想弄清楚如何识别注释的坐标是否在多边形内?

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let initialLocation = CLLocation(latitude: 49.140838, longitude: -123.127886)
        centerMapOnLocation(initialLocation)
        addBoundry()

        var annotation = MKPointAnnotation()
        annotation.coordinate = point1
        annotation.title = "Roatan"
        annotation.subtitle = "Honduras"

        mapView.addAnnotation(annotation)
    }

    var points = [CLLocationCoordinate2DMake(49.142677,  -123.135139),
                  CLLocationCoordinate2DMake(49.142730, -123.125794),
                  CLLocationCoordinate2DMake(49.140874, -123.125805),
                  CLLocationCoordinate2DMake(49.140885, -123.135214)]

    var point1 = CLLocationCoordinate2DMake(49.141821, -123.131577)

    func addBoundry() {
        let polygon = MKPolygon(coordinates: &points, count: points.count)

        mapView.addOverlay(polygon)
    }

    let regionRadius: CLLocationDistance = 1000

    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.magentaColor()

            return polygonView
        }

        return nil
    }
}

我创建了 @StefanS 答案的 Swift 版本,并通过从此移植代码使其更易于阅读answer

    func isPoint(point: MKMapPoint, insidePolygon poly: MKPolygon) -> Bool {

        let polygonVerticies = poly.points()
        var isInsidePolygon = false

        for i in 0..<poly.pointCount {
            let vertex = polygonVerticies[i]
            let nextVertex = polygonVerticies[(i + 1) % poly.pointCount]

            // The vertices of the edge we are checking.
            let xp0 = vertex.x
            let yp0 = vertex.y
            let xp1 = nextVertex.x
            let yp1 = nextVertex.y

            if ((yp0 <= point.y) && (yp1 > point.y) || (yp1 <= point.y) && (yp0 > point.y))
            {
                // If so, get the point where it crosses that line. This is a simple solution
                // to a linear equation. Note that we can't get a division by zero here -
                // if yp1 == yp0 then the above if be false.
                let cross = (xp1 - xp0) * (point.y - yp0) / (yp1 - yp0) + xp0

                // Finally check if it crosses to the left of our test point. You could equally
                // do right and it should give the same result.
                if cross < point.x {
                    isInsidePolygon = !isInsidePolygon
                }
            }
        }

        return isInsidePolygon
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MKPolygon - 如何确定 CLLocationCooperative2D 是否位于 CLLocationCooperative2D 多边形中? 的相关文章

  • xcode 6 资产目录 iPhone 6

    这个问题还没有得到解答 使用资产目录时 特定于设备 非通用 选项为 1x 2x r 2x 3x 1x 是不必要的 因为它不是视网膜 2x 是 ip4 的最佳选择 分辨率为 640x960 r 2x 适合 ip5 分辨率为 640x1136
  • SwiftUI 检测用户何时截取屏幕截图或屏幕录制

    On UIViewController我们可以轻松地将观察者添加到控制器 喜欢 class ViewController UIViewController override func viewDidLoad super viewDidLoa
  • Swift 结构类型集

    说我有一个struct 可以是任何东西 struct Cube var x Int var y Int var z Int var width Int 然后我该如何创建一个Set这些点中 是否存在两个具有相同属性的对象 let points
  • 如何使用群组在 iPhone 和 Apple Watch 之间共享数据?

    我是 Watchkit 开发新手 无法找到在 iPhone 和 iWatch 之间共享数据的解决方案 请帮助我 我希望使用组来共享数据 我们可以使用组在 iPhone 和 iWatch 之间传递数据 基本上iWatch不能做任何处理 我们需
  • iPad Safari Web Inspector 在页面加载时崩溃

    我有一个用 WordPress 制作的以视频为中心的网站 当我们在 iPad 上测试网站时 我们将其连接到 Mac 笔记本电脑并打开 safari 开发人员工具来检查是否有任何错误等 我们的主页上一切正常 但当我们转到我们添加的任何视频帖子
  • Swift 1.2 可选外部变量?

    由于 Swift 无法访问编译变量 因此我创建了一个指向编译变量的 Objective C extern 变量 编译变量转换器 h extern NSString const NetworkApiBasicAuthUsername 编译变量
  • UIAlertAction 处理程序在延迟后运行

    我正在尝试将 UIAlertViews 更改为 UIAlertControllers 我为此设置了这个操作 UIAlertAction undoStopAction UIAlertAction actionWithTitle Undo St
  • Objective C 宏附加到字符串

    我认为这是一件非常简单的事情 但由于我是 iOS 开发和 Objective C 的新手 所以我无法弄清楚 define RESTFUL PATH PREFIX https gogch com gch restful define LOGI
  • 如何在 Swift 中使用 deltaTime 正确计算 1 秒

    我正在尝试计算经过的秒数deltaTime但我不知道该怎么做 因为我的deltaTime不断打印 0 0166 或 0 0167 这是我的代码 override func update currentTime CFTimeInterval
  • 使用基于 Cookie 的身份验证的 Capacitor iOS

    我正在使用 Capacitor v3 NextJS 静态导出和 Django 后端基于生产网站构建 iOS 应用程序 当前的后端身份验证方案通过 cookie 使用 Django 会话 并通过 cookie 设置 CSRF 令牌 应用程序可
  • 使用什么来移动 UIView self.frame 或 self.transform 属性?

    这个问题我有点困惑 我知道我可以改变观点self frame origin 但我发现有一个名为 变换 的属性 我认为这是用于在屏幕上移动我的视图等 也许我有点不明白 所以我想知道 我必须使用哪个房产来搬家UIView 或者它的子类 另一个类
  • 在后台运行 URL 请求

    我想在一定的时间间隔内发出 url 请求 例如 每 10 分钟应用程序应该发出一次 url 调用并获取一些 json 数据 应用程序在后台运行时应该能够执行此操作 这可以做到吗 如果是这样 这是否违反 Apple 服务条款 有什么限制吗 i
  • 如何将 SCNPlane 颜色更改为透明颜色

    我正在开发一个 ARKit 项目 在水平面上点击时需要波纹动画效果 为此 我采用了 UIView 对象并将其作为 SCNPlane 对象材料的内容传递 我已将波纹动画添加到 UIView 对象 一切正常 但我无法将 SCNPlane 颜色更
  • 增量后清除推送通知徽章

    我正在研究 iPhone 中的推送通知 当我收到推送通知时 它在我的应用程序图标上显示 1 下次显示 2 3 4 如果我打开应用程序 它是 0 下次它应该是 1 2 3 4 但它显示最后一个数字和 1 我想在打开应用程序后重置推送通知徽章
  • 视频中的图像/文本叠加 swift

    我正在使用 swift 在视频中使用图像叠加来实现水印效果 我正在使用AVFoundation为此 但不知何故我没有成功 以下是我的覆盖图像 文本的代码 let path NSBundle mainBundle pathForResourc
  • UIImageWriteToSavedPhotosAlbum 选择器语法问题

    努力让 UIImageWriteToSavedPhotosAlbum 快速工作https developer apple com library ios documentation UIKit Reference UIKitFunction
  • admob ios7错误音频框架

    我正在将 admob SDK 当前的 集成到我的上一个应用程序 IOS7 Xcode5 中 并且出现了一个新错误 在新项目上也是如此 我想我错过了一些东西 但我多次重新启动该过程 但错误仍然存 在 Undefined symbols for
  • 在后台继续下载

    我正在创建一个应用程序 其中我从服务器下载一些数据 在后台运行时 我希望连接继续运行 以便可以下载数据 我知道有方法应用程序委托 void applicationDidEnterBackground UIApplication applic
  • 在 Swift 中,如何为具有自动布局的 UIView 制作动画,就像页面滑入一样?

    我尝试创建一个 UIView 来表示一个大小与设备屏幕相同的页面 由于该应用程序支持方向 因此我使用 AutoLayout 来构建它 它工作正常 直到我尝试将页面动画化以从右侧滑入 经过一番研究后 我能想到的最好的办法是 myView UI
  • 使用自定义格式将字符串转换为 NSDate [重复]

    这个问题在这里已经有答案了 可能的重复 NSString 到 NSDate https stackoverflow com questions 1353081 nsstring to nsdate iPhone 如何将 yyyyMMddTh

随机推荐