【问题标题】:How to set .xib interface as a custom callout view?如何将 .xib 界面设置为自定义标注视图?
【发布时间】:2015-04-22 11:29:54
【问题描述】:

我刚刚创建了带有 GUI 的 CustomCallout.xib。我想将其用作注释的自定义标注,但我不知道应该在哪里设置要显示的视图以及如何将值传递给该视图。

我想显示的标注视图如下所示。

这是我的自定义注释的代码。

class MapPin : MKPointAnnotation {
var name: String
var street: String
var type: String
var postCode: String

init(name: String, street: String, type: String, postCode: String) {
    self.name = name
    self.street = street
    self.type = type
    self.postCode = postCode
}}

还有我的视图控制器

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var mapView: MKMapView!
let span = MKCoordinateSpanMake(0.05, 0.05)


override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self

    let location = CLLocationCoordinate2D(
        latitude: 53.430,
        longitude: 14.529)

    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)

    let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
    punkt.coordinate = location

    mapView.addAnnotation(punkt)
}

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    if view.annotation.isKindOfClass(MKUserLocation){
        return
    }

    var customView = (NSBundle.mainBundle().loadNibNamed("CustomCallout", owner: self, options: nil))[0] as! CustomCallout;

    var calloutViewFrame = customView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    customView.frame = calloutViewFrame;

    let cpa = view.annotation as! MapPin

    view.addSubview(customView)

    var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: span)
    self.mapView.setRegion(newRegion, animated: true)
}}

不幸的是,正在运行的应用程序显示正确创建的 pin 具有正确的位置,但来自 let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand") 的值没有传递到视图,所以它看起来像这样。

【问题讨论】:

标签: ios objective-c xcode swift mapkit


【解决方案1】:

您可以在 Swift 中显示像这样的自定义标注视图。

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKPinAnnotationView!)
{
    if view.annotation.isKindOfClass(MKUserLocation){
        return
    }

    var customView = (NSBundle.mainBundle().loadNibNamed("SubView", owner: self, options: nil))[0] as CustomSubView;

    var calloutViewFrame = customView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    customView.frame = calloutViewFrame;

    let cpa = view.annotation as CustomPointAnnotation
   //You can add set vlaues for  here
   //cpa.title
   //cpa.postCode
   //cpa.street

    view.addSubview(customView)

    //zoom map to show callout
    let spanX = 0.0000000000000001
    let spanY = 0.0000000000000001

    var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
    self.map?.setRegion(newRegion, animated: true)
   }

【讨论】:

  • CustomPointAnnotation 里面是什么?
  • 它是 MKPointAnnotation 的自定义 Annotation 子类。具有图像、选定图像等的属性。在我的情况下,我想要自定义注释和标注。
  • 你已经设置了 annotation.title 和 annotation.subtitle。哪些显示在标注中。删除这些以隐藏您的标题和副标题。
  • 我刚刚用附加代码编辑了我的问题。如果您能看一下,我将不胜感激。
  • 您想在标注上显示名称街道类型和邮政编码吗?如果是这样,这些值应该在自定义标注类中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 2012-05-04
  • 2019-09-10
  • 1970-01-01
相关资源
最近更新 更多