最终编辑我几乎重建了所有类(自定义MKAnnotationView,自定义标注视图和另一个支持MKAnnotation协议的自定义类(这是我的注释类)),它只是善良的开始工作并显示标注。我把所有内容都归结为基本要素(即只使用自定义注释视图而不使用自定义标注,然后重新实现自定义标注)在测试项目中更清楚地看到事情,然后工作,我移植了我的结果回到我的实际项目。标注视图显示但不正确(我的自定义detailCalloutAccessoryView被切断,仍在尝试修复)。
当我点击自定义注释视图时,我的MapKit自定义标注视图不希望出现。我的代码出了什么问题?
这是我的“viewForAnnotation”函数...自定义注释视图工作正常,它只是没有出现的标注视图(根本没有)。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? RentalAnnotation {
let identifier = "pin"
var customAnnotationView: CustomAnnotationView
var customCalloutView = CustomCalloutView()
customCalloutView.backgroundColor = UIColor.greenColor()
let widthConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
customCalloutView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20)
customCalloutView.addConstraint(heightConstraint)
let image = UIImageView(image: UIImage(named: "bed"))
image.contentMode = .ScaleAspectFit
if let dequeuedView = rentalsMapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? CustomAnnotationView {
dequeuedView.annotation = annotation
dequeuedView.label!.text = "$ \(annotation.price)"
dequeuedView.canShowCallout = true
dequeuedView.detailCalloutAccessoryView = customCalloutView
customAnnotationView = dequeuedView
} else {
customAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
customAnnotationView.label?.text = "$ \(annotation.price)"
customAnnotationView.canShowCallout = true
customAnnotationView.detailCalloutAccessoryView = customCalloutView
customAnnotationView.calloutOffset = CGPoint(x: +5, y: 5)
}
return customAnnotationView
}
return nil
}
这是我的CustomCalloutView子类(UIView)的代码:
class CustomCalloutView: UIView {
@IBOutlet weak var calloutPropertyAvailabilityDate: UILabel!
@IBOutlet weak var calloutPropertyBedroomNumber: UILabel!
@IBOutlet weak var calloutPropertyBathroomNumber: UILabel!
var customCalloutView = UIView()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
customCalloutView = (NSBundle.mainBundle().loadNibNamed("CustomCalloutView", owner: self, options: nil)[0] as? UIView)!
self.addSubview(customCalloutView)
}
}
我设计了一个带有XIB文件的自定义标注视图界面,该文件为6个标签和一个按钮。
我做错了什么?我已经尝试了所有的东西......仍然无法让它发挥作用。
编辑 RentalAnnotation代码
class RentalAnnotation: NSObject, MKAnnotation {
var rentalName: String
var price: Int
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D
init(rentalName: String, price: Int, latitude: Double, longitude: Double, coordinate: CLLocationCoordinate2D) {
self.rentalName = rentalName
self.price = price
self.latitude = latitude
self.longitude = longitude
self.coordinate = coordinate
}
var title: String? {
return rentalName
}
var subtitle: String? {
return ("$\(price)")
}
func mapItem() -> MKMapItem {
let addressDictionary = [String(CNPostalAddressStreetKey): rentalName]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = rentalName
return mapItem
}
}
编辑2 didSelectAnnotationView方法的代码
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if ((view.annotation?.isKindOfClass(MKUserLocation)) == true) {
return
}
var customView = (NSBundle.mainBundle().loadNibNamed("CustomCalloutView", owner: self, options: nil))[0] as? CustomCalloutView
// var calloutFrame = customView?.frame
// calloutFrame?.origin = CGPointMake(-((calloutFrame?.size.width)!/2) + 15, -((calloutFrame?.size.height)!))
// customView?.frame = calloutFrame!
let cpa = view.annotation as? CustomPointAnnotation
//
// view.addSubview(customView!)
let spanX = 0.0000000000000001
let spanY = 0.0000000000000001
let newRegion = MKCoordinateRegion(center: (cpa?.coordinate)!, span: MKCoordinateSpanMake(spanX, spanY))
self.rentalsMapView.setRegion(newRegion, animated: true)
}
答案 0 :(得分:1)
尝试在注释中添加...
import socket
import struct
import json
import bluetooth
device = "10:00:E8:6B:F9:F9"
port = 1
btsocket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
print "Attempting to connect to " + device + " Port: " + str(port) + "...", btsocket.connect((device, port))
print "done."
while True:
try:
btbuffer = btsocket.recv(1024) ## 1024
except bluetooth.BluetoothError, b:
print "Bluetooth Error: ", b
。如果title
为title
,则不会显示标注。
来自文档:
SWIFT
var canShowCallout:Bool
Objective-C的
@property(非原子)BOOL canShowCallout如果此属性的值为YES,则标准的标注气泡为 当用户点击选定的注释视图时显示。标注使用 来自关联注释对象的标题和副标题文本。如果 但是,没有标题文本,注释视图被视为 其enabled属性设置为NO。标注也显示任何 存储在leftCalloutAccessoryView和中的自定义标注视图 rightCalloutAccessoryView属性。