Как установить наложение изображения в WKInterfaceMap поверх MapRect - PullRequest
0 голосов
/ 04 октября 2018

Я пытаюсь добавить наложение изображения на определенную область (скажем, Соединенные Штаты) в WKInterfaceMap объект Watchkit.В iOS с использованием MKMapView я могу добавить наложение изображения как:

У меня есть два пользовательских класса 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
}

}

ImageOverlayRenderer

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, поиска Google и документации Apple. В WKInterfaceMap мы можем добавить ограниченные аннотации, такие как булавки или изображения, для определенных location с centerOffSet.Я должен добавить наложение на регион (скажем, США).Пожалуйста, дайте мне знать, если это возможно или нет в Watch OS текущей среде.

Любая идея или направление будет принята с благодарностью.

...