我正在尝试在WKInterfaceMap
的{{1}}对象的特定区域(例如美国)上添加图像叠加层。在使用MKMapView的iOS中,我可以将图像叠加层添加为:
我有两个自定义类Watchkit
和ImageOverlay
ImageOverlayRenderer
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);
}
}
MKMapView代表将此绘制为:
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)
}
这完美地在我设置的坐标上绘制图像! 我的问题是:
如何使用这种技术或类似的方法来渲染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)
}
中的叠加层。根据我对SO线程,谷歌搜索和Apple文档的研究,我们可以在WatchKit WKInterfaceMap
中使用WKInterfaceMap
在特定的location
上添加有限的注释,例如图钉或图像。我必须在一个区域上添加覆盖(例如美国)。请让我知道在centerOffSet
当前环境中是否可行。
任何想法或方向将不胜感激。