【问题标题】:How create MKPolyline and MKPolygon with Swift?如何使用 Swift 创建 MKPolyline 和 MKPolygon?
【发布时间】:2016-05-01 03:18:03
【问题描述】:

我需要在地图上添加折线或多边形来创建菜单。 我知道如何使用注释列表渲染 MKPolyline 和 MKPolygon,但我不知道如何使用手势进行创建。

感谢您的帮助。

【问题讨论】:

    标签: swift mapkit mkannotation mkpolyline mkpolygon


    【解决方案1】:

    部分代码取自here

        let locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
        var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
        let polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
        let polygon = MKPolygon(coordinates: &coordinates, count: locations.count)
    
    
    let coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(polyline.pointCount)
    polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
    var coords: [Dictionary<String, AnyObject>] = []
    for i in 0..<polyline.pointCount {
        let latitude = NSNumber(double: coordsPointer[i].latitude)
        let longitude = NSNumber(double: coordsPointer[i].longitude)
        let coord = ["latitude" : latitude, "longitude" : longitude]
        coords.append(coord)
    }
    print(coords)
    

    【讨论】:

    • 我知道如何渲染,但我不知道如何用手指获取坐标。
    • @Z.S.我确实更新了如何从折线获取坐标的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多