iOS Swift MapKit自定义注释 [英] iOS Swift MapKit Custom Annotation

查看:163
本文介绍了iOS Swift MapKit自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试放置一个图钉时,我在设置地图视图中加载自定义注释时遇到了一些麻烦。

I am having some trouble getting a custom annotation to load inside of my map view when I try to place a pin.

import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
@IBAction func ReportBtn(sender: AnyObject) {
    //MARK: Report Date And Time Details
    let ReportTime = NSDate()
    let TimeStamp = NSDateFormatter()
    TimeStamp.timeStyle = NSDateFormatterStyle.ShortStyle
    TimeStamp.dateStyle = NSDateFormatterStyle.ShortStyle
    TimeStamp.stringFromDate(ReportTime)
    //MARK: Default Point Annotation Begins
    let ReportAnnotation = MKPointAnnotation()
    ReportAnnotation.title = "Annotation Created"
    ReportAnnotation.subtitle = ReportTime.description
    ReportAnnotation.coordinate = locationManager.location!.coordinate
    mapView(MainMap, viewForAnnotation: ReportAnnotation)
    MainMap.addAnnotation(ReportAnnotation)
}

@IBOutlet weak var MainMap: MKMapView!
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.startUpdatingLocation()
    self.MainMap.showsUserLocation = true
}


//MARK: - Location Delegate Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
 let location = locations.last
 let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
 let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02 ))
    self.MainMap.setRegion(region, animated: true)
    //self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError){
    print(error.localizedDescription)
}
//MARK:Custom Annotation Begins Here
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    guard !annotation.isKindOfClass(MKUserLocation) else {
        return nil
    }
    /*if annotation.isKindOfClass(MKUserLocation){
        //emty return, guard wasn't cooperating
    }else{
    return nil
    }*/
    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier){
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else{
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        annotationView = av
    }
    if let annotationView = annotationView {
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "image.png")
    }
    return annotationView

}
}



已添加信息



我很肯定按钮功能非常完美。使用上面转储的当前代码,默认的红色引脚注释会出现在它应该的位置。当我点击引脚时,我指定的描述也没有问题。 我对这段代码唯一的问题就是我无法让我的图像取代无聊的默认红色图钉

推荐答案

我建议继承`MKPointAnnotation。

I recommend subclassing `MKPointAnnotation.

我已经包含了只显示自定义地图图钉的必要代码。将其视为模板

I have included only the necessary code to display a custom map pin. Think of it as a template.

大纲


  • 我们将创建一个点注释对象,并使用 CustomPointAnnotation 类分配自定义图像名称。

  • We will create a point annotation object and assigning a custom image name with the CustomPointAnnotation class.

我们将子类化 MKPointAnnotation 来设置图像并在委托协议方法上分配 viewForAnnotation

We will subclass the MKPointAnnotation to set image and assign it on the delegate protocol method viewForAnnotation.

在使用标题和副标题设置点注释的坐标后,我们将向地图添加注释视图。

We will add an annotation view to the map after setting the coordinate of the point annotation with a title and a subtitle.

我们将实现 viewForAnnotation 方法,该方法是 MKMapViewDelegate 调用引脚在地图上显示的协议方法。 viewForAnnotation 协议方法是自定义图钉视图并为其指定自定义图像的最佳位置。

We will implement the viewForAnnotation method which is an MKMapViewDelegate protocol method which gets called for pins to display on the map. viewForAnnotation protocol method is the best place to customise the pin view and assign a custom image to it.

我们将出列并返回给定标识符的可重用注释,并将注释强制转换为我们的自定义 CustomPointAnnotation 类,以便访问图像名称引脚。

We will dequeue and return a reusable annotation for the given identifier and cast the annotation to our custom CustomPointAnnotation class in order to access the image name of the pin.

我们将在 Assets.xcassets 中创建一个新的图像集并放置图像@ 3x相应地.png和image@2x.png。

We will create a new image set in Assets.xcassets and place image@3x.png and image@2x.png accordingly.

不要忘记plist。

NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription

一如既往地在真实设备上进行测试。

As always test on a real device.

这篇关于iOS Swift MapKit自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆