将 MKPolygon Objective-C 代码转换为 Swift
Posted
技术标签:
【中文标题】将 MKPolygon Objective-C 代码转换为 Swift【英文标题】:Converting MKPolygon Objective-C code to Swift 【发布时间】:2015-01-29 06:46:44 【问题描述】:我有以下 Objective-C 方法:
- (void)addPolygonToMap
NSInteger numberOfPoints = [self.coordinates count];
if (numberOfPoints > 4)
CLLocationCoordinate2D points[numberOfPoints];
for (NSInteger i = 0; i < numberOfPoints; i++)
points[i] = [self.coordinates[i] MKCoordinateValue];
self.polygon = [MKPolygon polygonWithCoordinates:points count:numberOfPoints];
[self.mapView addOverlay: self.polygon];
self.isDrawingPolygon = NO;
[self.drawPolygonButton setTitle:@"draw" forState:UIControlStateNormal];
self.canvasView.image = nil;
[self.canvasView removeFromSuperview];
我尝试将其转换为 Swift:
func addPolygonToMap()
var numberOfPoints: NSInteger = self.coordinates.count
if (numberOfPoints > 4)
var points: [CLLocationCoordinate2D] = []
var coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(numberOfPoints)
for i in 0..<numberOfPoints
points.append(coordsPointer[i])
self.polygon = MKPolygon(coordinates: &points, count: numberOfPoints)
self.mapView.addOverlay(self.polygon)
coordsPointer.dealloc(numberOfPoints)
self.isDrawingPolygon = false
self.drawPolygonButton.setTitle("Draw", forState: .Normal)
self.canvasView.image = nil
self.canvasView.removeFromSuperview()
最后,当调用委托方法时,它实际上并没有将overlay
添加到mapView
。我什么都看不到。
我假设这是我的 self.addPolytonToMap()
方法,但我不是 100% 确定。整个场景在我的 Objective-C 项目中运行良好。
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!
if (overlay is MKPolygon)
var overlayPathView = MKPolygonRenderer(overlay: overlay)
overlayPathView.fillColor = UIColor.cyanColor().colorWithAlphaComponent(0.2)
overlayPathView.strokeColor = UIColor.cyanColor().colorWithAlphaComponent(0.2)
overlayPathView.lineWidth = 5
return overlayPathView
else if (overlay is MKPolyline)
var overlayPathView = MKPolylineRenderer(overlay: overlay)
overlayPathView.strokeColor = UIColor.blueColor().colorWithAlphaComponent(0.7)
overlayPathView.lineWidth = 5
return overlayPathView
return nil
更新:
我刚刚注意到在我的 Objective-C 版本中,points[i].latitude
可以正常通过,但是当我执行以下操作时,我得到了一个奇怪的输出:
println(coordsPointer[i].latitude)
输出:
1.63041663127611e-321
1.64523860065135e-321
1.65511991356818e-321
1.68970450877706e-321
1.7045264781523e-321
1.72922976044436e-321
这可以解释为什么我看不到overlay
,但是我对UnsafeMutablePointer<>
的经验有限。
【问题讨论】:
我只是快速浏览了您的代码 - 您似乎正在分配 coordsPointer 并且从未填充它。但是然后您从坐标指针设置点...但实际上您应该必须从 self.coordinates[i] 读取值 【参考方案1】:通过修改addPolygonToMap()
方法修复:
func addPolygonToMap()
var numberOfPoints: NSInteger = self.coordinates.count
if (numberOfPoints > 4)
var points: [CLLocationCoordinate2D] = []
for i in 0..<numberOfPoints
points.insert(self.coordinates[i].MKCoordinateValue, atIndex: i)
self.polygon = MKPolygon(coordinates: &points, count: numberOfPoints)
self.mapView.addOverlay(self.polygon)
self.isDrawingPolygon = false
self.drawPolygonButton.setTitle("Draw", forState: .Normal)
self.canvasView.image = nil
self.canvasView.removeFromSuperview()
感谢 @Volker 的帮助。
【讨论】:
以上是关于将 MKPolygon Objective-C 代码转换为 Swift的主要内容,如果未能解决你的问题,请参考以下文章
如何向 MKPolyline 和 MKPolygon 添加描述?
Objective-C 协议在 XCTest 中选择器失败,但在应用程序中失败 [重复]
iOS - Objective-C 关联(objc_setAssociatedObjectobjc_getAssociatedObjectobjc_removeAssociatedObjects)(代
如何区分几个不同的 MKPolygon 叠加层,以便我可以为每个叠加层赋予不同的属性?