首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift - MKPointAnnotation定制引脚图像

Swift - MKPointAnnotation定制引脚图像
EN

Stack Overflow用户
提问于 2018-07-08 22:55:39
回答 1查看 3.6K关注 0票数 1

我使用Swift 3和Xcode 10 beta 3,我需要在地图上为我的引脚使用自定义图像。我需要避免红色注解引脚,并使用一些自定义的标志,我为这一点。我尝试了堆叠中找到的所有解决方案,但没有任何帮助。这是我的密码:

代码语言:javascript
复制
import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var map: MKMapView!

    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.locationManager = CLLocationManager()
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()

        //let span:MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
        let locationANTOMI:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.4509339, 9.1713609)
        let locationANTOTO:CLLocationCoordinate2D = CLLocationCoordinate2DMake(45.06666, 7.68826)
        //let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        //map.setRegion(region, animated: true)
        let annotationANTOMI = MKPointAnnotation()
        annotationANTOMI.coordinate = locationANTOMI
        annotationANTOMI.title = "ANTONIOLI MILANO"

        map.addAnnotation(annotationANTOMI)

        let annotationANTOTO = MKPointAnnotation()
        annotationANTOTO.coordinate = locationANTOTO
        annotationANTOTO.title = "ANTONIOLI TORINO"
        map.addAnnotation(annotationANTOTO)
    }
}

我怎么发动汽车呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-08 23:50:36

我通常的做法是创建一个新的快速文件,它将是您从MKAnnoatation继承的自定义注释。下面的例子

代码语言:javascript
复制
import MapKit

class MyAnnotation: NSObject, MKAnnotation {
    let title: String?
    let subtitle: String?
    let coordinate: CLLocationCoordinate2D
    var image: UIImage? = nil

    init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
        //self.image
        super.init()
    }
}

在必须使用CLCoordinate的地方,您需要初始化注释。然后,使用自定义图像设置图像属性。MyAnnotation.image = "myImage.png". You will then need to add your annotation to your map viewmapView.addAnnotations(MyAnnotation).我还从MKMapViewDelegate实现了下面的方法(确保您在类中继承了这个方法)。这样用户就可以点击注释并接收有关它的信息。希望这能有所帮助。

在您的视图中,控制器:

代码语言:javascript
复制
let marker = MyAnnotation(title: "title" as! String, subtitle: "subtitle" as! String, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
marker.image = UIImage("my image.png")
self.mapView.addAnnotations(marker)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? MyAnnotation {
        let identifier = "identifier"
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.image = annotation.image //add this
        annotationView?.canShowCallout = true
        annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
        return annotationView
    }
    return nil
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51236577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档