Meshtastic-Apple/MeshtasticApple/Views/Map/Custom/PositionAnnotationView.swift

64 lines
1.7 KiB
Swift
Raw Normal View History

//
// PositionAnnotationView.swift
// MeshtasticApple
//
// Created by Joshua Pirihi on 24/12/21.
//
import UIKit
import MapKit
2021-12-25 23:48:12 -08:00
// a simple circle annotation, with a string in it
class PositionAnnotation: NSObject, MKAnnotation {
2021-12-25 23:48:12 -08:00
// This property must be key-value observable, which the `@objc dynamic` attributes provide.
@objc dynamic var coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
2021-12-25 23:48:12 -08:00
// Required if you set the annotation view's `canShowCallout` property to `true`
2021-12-25 23:48:12 -08:00
// this string fills the callout label when you tap an annotation
var title: String?
2021-12-25 23:48:12 -08:00
// the text to appear inside the little circle
var shortName: String?
2021-12-25 23:48:12 -08:00
}
class PositionAnnotationView: MKAnnotationView {
private let annotationFrame = CGRect(x: 0, y: 0, width: 32, height: 32)
2022-01-10 06:09:31 +13:00
private let label: UILabel
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
self.label = UILabel(frame: annotationFrame.offsetBy(dx: 0, dy: 0))
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
self.frame = annotationFrame
self.label.font = UIFont.preferredFont(forTextStyle: .caption2)
self.label.textColor = .white
self.label.textAlignment = .center
self.backgroundColor = .clear
self.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) not implemented!")
}
public var name: String = "" {
didSet {
self.label.text = name
}
2022-01-10 06:09:31 +13:00
}
2022-01-10 06:09:31 +13:00
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
2022-01-10 06:09:31 +13:00
let circleRect = CGRect(x: 1, y: 1, width: 30, height: 30)
2022-01-10 06:09:31 +13:00
context.setFillColor(CGColor(red: 0, green: 0.5, blue: 1.0, alpha: 1.0))
2022-01-10 06:09:31 +13:00
context.fillEllipse(in: circleRect)
2021-12-25 23:48:12 -08:00
2022-01-10 06:09:31 +13:00
}
}