我尝试在MapKit和SwiftUI中自定义我的注释
从下面的代码中,我在地图上搜索特定的坐标(坐标),并显示自定义注释。
1)我试图增加UIimage的大小,因为要变小(请参阅所附图片)并更改颜色,知道如何吗?
2)在应用程序启动后的地图中,它仅显示图标,在我点击图标后,注释会出现,您知道如何立即显示我的注释而不点击吗?
3)现在在注释中,我设法显示标题和副标题,如何同时显示坐标
struct MapView: UIViewRepresentable {
let Mm : MapManager
let coord = CLLocationCoordinate2D(latitude: 52.28792, longitude: 4.73415327)
class Coordinator: NSObject, MKMapViewDelegate {
var parent : MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "TESTING NOTE")
annotationView.canShowCallout = true
annotationView.image = UIImage(systemName: "location.circle")
return annotationView
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> MKMapView {
let view = MKMapView(frame: .zero)
Mm.georeverseCoordinate(coord) { (pin) in
if let pinOK = pin {
view.removeAnnotation(pinOK)
view.mapType = MKMapType.satellite
let span = MKCoordinateSpan(latitudeDelta: 0.04, longitudeDelta: 0.04)
let region = MKCoordinateRegion(center: self.coord, span: span)
view.setRegion(region, animated: true)
view.delegate = context.coordinator
view.addAnnotation(pinOK)
}
}
return view
}
func updateUIView(_ view: MKMapView, context: Context) {
}
}
地图管理员
class MapManager: NSObject, CLLocationManagerDelegate {
static let shared = MapManager()
func georeverseCoordinate(_ coord: CLLocationCoordinate2D , closure: @escaping (Pin?) -> Void) {
let location = CLLocation(latitude: coord.latitude, longitude: coord.longitude)
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (arrayResponse, error) in
if let errorTest = error {
debugPrint(errorTest.localizedDescription)
closure(nil)
return
}
if let arrayPins = arrayResponse {
if let valorePinArray = arrayPins.first {
debugPrint(valorePinArray.locality!)
debugPrint(valorePinArray.isoCountryCode!)
let pin = Pin(title: valorePinArray.locality!, subtitle: valorePinArray.isoCountryCode!, coordinate: valorePinArray.location!.coordinate)
closure(pin)
}
else { closure(nil) }
}
else { closure(nil) }
}
}
}
引脚模型
class Pin:NSObject, MKAnnotation {
var title : String?
var subtitle : String?
var coordinate : CLLocationCoordinate2D
var color: UIColor?
init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}
}
1]-似乎存在一个错误,使得使用tintColor
和特定的渲染模式很难更改SF符号的颜色,但是有一种解决方法也可以通过一种简便的方法来更改符号的大小。
在mapView:viewFor annotation
中添加以下内容:
annotationView.canShowCallout = true
annotationView.image = UIImage(systemName: "location.circle")?.withTintColor(.systemGreen, renderingMode: .alwaysOriginal)
let size = CGSize(width: 40, height: 40)
annotationView.image = UIGraphicsImageRenderer(size:size).image {
_ in annotationView.image!.draw(in:CGRect(origin:.zero, size:size))
}
2)-要在标注添加到地图后立即显示标注,请使用selectAnnotation
。
view.addAnnotation(pinOK)
view.selectAnnotation(pinOK, animated: true)
3)-构造注释时可使用坐标,因此您只需在Pin类中更改init:
init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
self.title = "\(coordinate.latitude) : \(coordinate.longitude)"
self.subtitle = subtitle
}