【问题标题】:MKMapRectMake in Swift causes linker error when compilingSwift 中的 MKMapRectMake 在编译时导致链接器错误
【发布时间】:2026-01-08 05:00:01
【问题描述】:

使用 MKMapRectMake 创建 MKMapRect 导致编译错误如下:

这是我的代码:

    var lat = 37.33072
    var lon = -122.029674
    var loc = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    var point = MKMapPointForCoordinate(loc)

    var flyTo = MKMapRectMake(point.x, point.y, 0, 0);

这是编译器的错误:

Undefined symbols for architecture i386:
  "_MKMapPointMake", referenced from:
      _MKMapRectMake in ViewController.o
  "_MKMapSizeMake", referenced from:
      _MKMapRectMake in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我的解决方法是使用 origin 和 size 参数创建 MKMapRect。请注意,我已将 MKMapKit 添加到构建阶段中的链接库中

有没有人遇到同样的问题以及如何解决这个问题?

【问题讨论】:

  • 确保参数与函数接受的类型相同。它们必须是 Double 值。
  • 似乎是当前测试版中的一个错误。您可以尝试this answer 中显示的 Objective-C/Swift 桥接解决方法作为临时修复。本质上,在 Objective-C 中调用 MKMapRect 并从 Swift 中调用你的桥接方法。
  • 谢谢。这可能是另一种尝试。

标签: swift mapkit xcode6


【解决方案1】:

使用实用功能解决它:

func myMKMapRect(x: Double, y:Double, w:Double, h:Double) -> MKMapRect {
    return MKMapRect(origin:MKMapPoint(x:x, y:y), size:MKMapSize(width:w, height:h))
}

当然,还要向 Apple 提交错误报告。

【讨论】:

    【解决方案2】:

    改变这个 var flyTo = MKMapRectMake(point.x, point.y, 0, 0);

    var flyTo = MKMapRect(origin: MKMapPointMake(x:point.x, y: point.y),size MKMapSizeMake(width:0, height: 0))

    MKMapRect 有一个 origin 和 size 参数,你错过了

    MKMapRect(origin: MKMapPointMake(x:, y: ),size MKMapSizeMake(width: , height: ))

    【讨论】:

      【解决方案3】:

      变量坐标:CLLocationCoordinate2D var boundingMapRect : MKMapRect

      这两个是 MKOverlay 的必需属性,添加它们。

      【讨论】: