如何使用 MKPolygon 的 Apple Map 绘制多边形

我在以下坐标的帮助下创建了一个多边形。多边形在地图上看起来不错,但我想知道多边形的形状。在下面的代码中,我写了一个多边形。

let points1 = CLLocationCoordinate2DMake(24.63743783432579,77.323397508938)
let points12 = CLLocationCoordinate2DMake(24.65085603700094,77.316960207723)

let points13 = CLLocationCoordinate2DMake(24.64453717929222,77.309578768996)
let points14 = CLLocationCoordinate2DMake(24.64640946676303,77.336872926149)

coordinates.append(points1)
coordinates.append(points12)
coordinates.append(points13)
coordinates.append(points14)
print(coordinates.count)
let polygon = MKPolygon(coordinates: &coordinates,count: coordinates.count)
polygon.title = "33"
polygon.subtitle = ""
mapView.addOverlay(polygon)

我已经使用委托方法编写了以下代码来渲染多边形。

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation,reuseIdentifier: identifier)
        annotationView!.image =  some_image
        annotationView!.canShowCallout = true
    }
    else {
        annotationView!.annotation = annotation
        annotationView!.image = some_image
    }
    return annotationView
}

我在这里附上了两张图片,它们在 UI 上显示了最终结果,这不是预期的。

实际图片

如何使用 MKPolygon 的 Apple Map 绘制多边形

预期:

如何使用 MKPolygon 的 Apple Map 绘制多边形

我是否遗漏了地图属性中的某些内容?

ks_xpd 回答:如何使用 MKPolygon 的 Apple Map 绘制多边形

因为坐标的顺序,你得到了那个形状。

例如,您已按以下顺序为我们提供了四个坐标(尽管您的地图有五个):

let coordinates = [
    CLLocationCoordinate2DMake(24.63743783432579,77.323397508938),CLLocationCoordinate2DMake(24.65085603700094,77.316960207723),CLLocationCoordinate2DMake(24.64453717929222,77.309578768996),CLLocationCoordinate2DMake(24.64640946676303,77.336872926149)
]

让我们展示叠加层(并标记坐标):

enter image description here

如果你固定坐标的顺序......

let coordinates = [
    CLLocationCoordinate2DMake(24.63743783432579,77.336872926149)
]

然后你会得到一个你期望的多边形:

enter image description here

现在,在您的图像中,您有五个坐标,但问题是相同的,即坐标的顺序不正确(并且由于多边形与自身相交,它应用了切出中心的默认填充规则)。但固定坐标的顺序,使它们描边多边形的周长,问题将得到解决。

本文链接:https://www.f2er.com/307321.html

大家都在问