gpt4 book ai didi

ios - Swift - 带有图像的自定义 MKPointAnnotation

转载 作者:搜寻专家 更新时间:2023-10-30 22:22:45 24 4
gpt4 key购买 nike

我正在尝试创建自定义 MKPointAnnotation 以在 map View 上使用。它看起来非常像 Apple 照片中使用的那个:

enter image description here

我将从服务器中检索一些照片及其位置。然后我想显示一个像上面那样的注释,注释中包含图像。

我目前有一个程序可以在正确的坐标处添加一个普通的MKPointAnnotation,也可以从服务器上获取相关的照片。

我想要的只是将我的 MKPointAnnotation 设计成那样。

我尝试过遵循其他答案,但我认为这略有不同,因为我想每次都在模板上显示图像。

最佳答案

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 来自 MKMapViewDelegate 是您需要覆盖的函数。

它允许您为每个注释提供“自定义” View 。在此函数中,您可以将自定义 View (MKAnnotationView 的子类)出列并设置自定义属性,或者您可以将具有属性 image< 的常规 MKAnnotationView 出列.

您可以设置该属性以显示自定义图像。无论如何,我宁愿使用自己的 annotationView,因为您可以添加自定义布局(标签、 ImageView 等)和主题(颜色、图层等)。

例子:

//
// ViewController.swift
// Maps
//
// Created by Brandon T on 2017-02-20.
// Copyright © 2017 XIO. All rights reserved.
//

import UIKit
import MapKit


class ImageAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var image: UIImage?
var colour: UIColor?

override init() {
self.coordinate = CLLocationCoordinate2D()
self.title = nil
self.subtitle = nil
self.image = nil
self.colour = UIColor.white
}
}

class ImageAnnotationView: MKAnnotationView {
private var imageView: UIImageView!

override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

self.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.addSubview(self.imageView)

self.imageView.layer.cornerRadius = 5.0
self.imageView.layer.masksToBounds = true
}

override var image: UIImage? {
get {
return self.imageView.image
}

set {
self.imageView.image = newValue
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class ViewController: UIViewController, MKMapViewDelegate {

var mapView: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
super.viewDidLoad()


self.initControls()
self.doLayout()
self.loadAnnotations()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func initControls() {
self.mapView = MKMapView()

self.mapView.isRotateEnabled = true
self.mapView.showsUserLocation = true
self.mapView.delegate = self

let center = CLLocationCoordinate2DMake(43.761539, -79.411079)
let region = MKCoordinateRegionMake(center, MKCoordinateSpanMake(0.005, 0.005))
self.mapView.setRegion(region, animated: true)
}

func doLayout() {
self.view.addSubview(self.mapView)
self.mapView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
self.mapView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
self.mapView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.mapView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
self.mapView.translatesAutoresizingMaskIntoConstraints = false
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) { //Handle user location annotation..
return nil //Default is to let the system handle it.
}

if !annotation.isKind(of: ImageAnnotation.self) { //Handle non-ImageAnnotations..
var pinAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "DefaultPinView")
if pinAnnotationView == nil {
pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "DefaultPinView")
}
return pinAnnotationView
}

//Handle ImageAnnotations..
var view: ImageAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "imageAnnotation") as? ImageAnnotationView
if view == nil {
view = ImageAnnotationView(annotation: annotation, reuseIdentifier: "imageAnnotation")
}

let annotation = annotation as! ImageAnnotation
view?.image = annotation.image
view?.annotation = annotation

return view
}


func loadAnnotations() {
let request = NSMutableURLRequest(url: URL(string: "https://i.imgur.com/zIoAyCx.png")!)
request.httpMethod = "GET"

let session = URLSession(configuration: URLSessionConfiguration.default)
let dataTask = session.dataTask(with: request as URLRequest) { (data, response, error) in
if error == nil {

let annotation = ImageAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(43.761539, -79.411079)
annotation.image = UIImage(data: data!, scale: UIScreen.main.scale)
annotation.title = "Toronto"
annotation.subtitle = "Yonge & Bloor"


DispatchQueue.main.async {
self.mapView.addAnnotation(annotation)
}
}
}

dataTask.resume()
}
}

关于ios - Swift - 带有图像的自定义 MKPointAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42351358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com