3

我正在尝试WKInterfaceMapWatchkit. 在使用 MKMapView 的 iOS 中,我可以将图像叠加添加为:

我有两个自定义类ImageOverlay&ImageOverlayRenderer

class ImageOverlay: NSObject, MKOverlay {

(topLeftCoordinate: CLLocationCoordinate2D, bottomRightCoordinate: CLLocationCoordinate2D,
     topRightCoordinate: CLLocationCoordinate2D, bottomLeftCoordinate:CLLocationCoordinate2D
     ) {
       // set coordinates here
    }

    var midPointCoordinate: CLLocationCoordinate2D {
    get {
        return CLLocationCoordinate2D(latitude: (topLeftCoordinate.latitude + bottomRightCoordinate.latitude)/2, longitude: (topLeftCoordinate.longitude + bottomRightCoordinate.longitude)/2)
    }
}

func overlayBoundingMapRect() -> MKMapRect {
    let topLeft = MKMapPointForCoordinate(self.topLeftCoordinate)
    let topRight = MKMapPointForCoordinate(self.topRightCoordinate)
    let bottomLeft = MKMapPointForCoordinate(self.bottomLeftCoordinate)
    let bottomRight = MKMapPointForCoordinate(self.bottomRightCoordinate)


    return MKMapRectMake(topLeft.x,
                         topLeft.y,
                         fabs(topLeft.x - topRight.x),
                         fabs(((topLeft.y + topRight.y) / 2.0 ) - ((bottomLeft.y + bottomRight.y) / 2.0 )));
}

//MKOverlay protocol

var boundingMapRect: MKMapRect {
    return overlayBoundingMapRect()
}

var coordinate: CLLocationCoordinate2D {
    return midPointCoordinate
}

}

图像叠加渲染器

class ImageOverlayRenderer: MKOverlayRenderer {
    init(overlay: MKOverlay, overlayImages: [UIImage]) {
    self.overlayImages = overlayImages[0]
    super.init(overlay: overlay)
}

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        let imageReference: CGImage = self.overlayImages[0].cgImage!;

        let theMapRect: MKMapRect = self.overlay.boundingMapRect;
        let theRect: CGRect = rect(for: theMapRect);

        context.scaleBy(x: 1.0, y: -1.0);
        context.translateBy(x: 0.0, y: -theRect.size.height);
        context.draw(imageReference, in: theRect);

}

}

这就是我使用这些类来渲染叠加层的方式:

var circleMapView : MKMapView()
var mapImageOverlay : ImageOverlay?
var circleMapImageRenderer : ImageOverlayRenderer?

// fix coordinates to cover US region
let topLeft = CLLocationCoordinate2D(latitude: topLeftCoordinate.0, longitude: topLeftCoordinate.1)
let bottomRight = CLLocationCoordinate2D(latitude: bottomRightCoordinate.0, longitude: bottomRightCoordinate.1)

let topRight = CLLocationCoordinate2D(latitude: topRightCoordinate.0, longitude: topRightCoordinate.1)
let bottomLeft = CLLocationCoordinate2D(latitude: bottomLeftCoordinate.0, longitude: bottomLeftCoordinate.1)

 mapImageOverlay = ImageOverlay(topLeftCoordinate: topLeft , bottomRightCoordinate: bottomRight, topRightCoordinate:topRight, bottomLeftCoordinate: bottomLeft)

if let overlay = mapImageOverlay {
        circleMapView.insert(overlay, at: 0)
    }

MKMapView 委托将其绘制为:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is ImageOverlay
    {
        let renderer = ImageOverlayRenderer(overlay: imageOverlay, overlayImages: imgs)
            circleMapImageRenderer = renderer
            return renderer
    }
return MKOverlayRenderer(overlay: overlay)
}

这完美地在我设定的坐标上绘制图像!我的问题是:

如何使用这种技术或类似的东西在WatchKit WKInterfaceMap. 根据我对 SO 线程、谷歌搜索和 Apple 文档的研究,WKInterfaceMap我们可以在特定locationcenterOffSet. 我必须在一个地区(比如美国)上添加叠加层。请让我知道这在Watch OS当前环境中是否可行。

任何想法或方向将不胜感激。

4

0 回答 0