- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用带有 MKPointAnnotation 的 MKMapView(代理设置正确)。注释是在后台线程上调用的此方法中生成的。
func updateMapAnnotations() {
for var i = 0; i < DataManager.getStationList().count; i++ {
var s = DataManager.getStationList()[i] as Station
var annotation = MKPointAnnotation()
annotation.setCoordinate(CLLocationCoordinate2D(latitude: s.latitude, longitude: s.longitude))
annotation.title = "\(s.id)"
dispatch_async(dispatch_get_main_queue(), {
self.mapView.addAnnotation(annotation)
})
}
}
注解 View 在这里生成:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "StationAnnotation"
let annoStation = DataManager.getStationById(annotation.title!.toInt()!)
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26))
let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22))
imageView.image = UIImage(named: "test")
base.layer.cornerRadius = 3.0
base.clipsToBounds = true
base.backgroundColor = UIColor.whiteColor()
var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25))
priceLabelBig.textColor = UIColor.blackColor()
priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15)
var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15))
priceLabelSmall.textColor = UIColor.blackColor()
priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12)
if let curPrice = annoStation?.getTextWithSettings().description {
var stringLength = countElements(curPrice)
var substringToIndex = stringLength - 1
priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex))
priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex))
}
base.addSubview(imageView)
base.addSubview(priceLabelBig)
base.addSubview(priceLabelSmall)
anView.addSubview(base)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
我知道我必须设置注释标题并将“canShowCallOut”设置为 true 才能使“didSelectAnnotationView”正常工作。如您所见,两者都设置正确。
所以我有一个 mapView(委托(delegate)已设置),“canShowCallOut”为真,标题已设置且可以正常工作。
要进入详细信息页面,我想跟踪对 annotationViews ('didSelectAnnotationView') 的点击,但它没有被调用。
我做错了什么?
最佳答案
好吧,我自己找到了解决方案。
您必须明确设置 annotationViews 框架。如果您只是为 View 设置 subview ,它们将被显示,但 View 框架设置为 0、0(高度、重量)。所以你不能点击它,因为区域也是 0, 0。
我的解决方案是这样的,有趣的是 annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26)
。其他一切都一样。现在单击注释调用 didSelectAnnotationView
。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "stationAnnotation"
let annoStation = DataManager.getStationById(annotation.title!.toInt()!)
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
annotationView.frame = CGRect(x: 0, y: 0, width: 67, height: 26)
let base = UIView(frame: CGRect(x: 0, y: 0, width: 67, height: 26))
base.userInteractionEnabled = true
let imageView = UIImageView(frame: CGRect(x: 2, y: 2, width: 22, height: 22))
imageView.image = UIImage(named: "zapfsaeule")
base.layer.cornerRadius = 3.0
base.clipsToBounds = true
base.backgroundColor = UIColor.whiteColor()
var priceLabelBig = UILabel(frame: CGRect(x: 25, y: 0, width: 30, height: 25))
priceLabelBig.textColor = UIColor.blackColor()
priceLabelBig.font = UIFont(name: priceLabelBig.font.fontName, size: 15)
var priceLabelSmall = UILabel(frame: CGRect(x: 55, y: 0, width: 12, height: 15))
priceLabelSmall.textColor = UIColor.blackColor()
priceLabelSmall.font = UIFont(name: priceLabelBig.font.fontName, size: 12)
if let curPrice = annoStation?.getPriceWithSettings().description {
var stringLength = countElements(curPrice)
var substringToIndex = stringLength - 1
priceLabelBig.text = curPrice.substringToIndex(advance(curPrice.startIndex, substringToIndex))
priceLabelSmall.text = curPrice.substringFromIndex(advance(curPrice.startIndex, substringToIndex))
}
base.addSubview(imageView)
base.addSubview(priceLabelBig)
base.addSubview(priceLabelSmall)
annotationView.addSubview(base)
annotationView.annotation = annotation
}
else {
annotationView.annotation = annotation
}
return annotationView
}
关于ios - 未调用 MKMapView MKPointAnnotation 点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669444/
我想用自定义 Logo 替换默认的 MKPointAnnotation Logo 。 所以我在 MapViewController 中编写了这段代码: import UIKit import MapK
我正在尝试向我的 MKPointAnnotations 添加一个按钮,然后对新的 View Controller 执行操作 我仔细研究了这两个问题 How to add button to MKPoi
简单问题:我似乎找不到 MKPointAnnotation 类的标记属性。它返回错误; MKPointAnnotation *annotation = [[HCIAnnotationViewContr
所以我有以下代码块,它应该迭代 JSON 对象数组并将 MKPointAnnotations 放在 map 上: for(id jsonObject in dataArray) { NSLog
我有 map View ,其中几乎没有 MKPointAnnotation。 一切正常,但是, View 的 MKPoiintAnnotation 的“背景”是“不可见的”,因此不是很“可见”。 我想
我是 Objective-C 的大三学生。我有一个任务将手势识别器添加到 MKPointAnnotation。我听说可以通过 MKMapView 委托(delegate)来完成。但我还不明白。 因此,
我创建了很多图钉,当我按下图钉时,标题必须显示,而副标题必须隐藏,因为它是一段很长的文本并且出现在 UItextView 中。问题是我没有找到隐藏字幕的方法,所以在标题下,我有一段很长的文字,结尾是:
是否可以向 MKPointAnnotation 添加额外的属性?目前有 coordinate , title , 和 subtitle . 是否可以添加 url可以在 -(MKAnnotationVi
我需要更改 MKPointAnnotation 对象的颜色,但我编写的方法似乎只生成红色图钉。该方法工作正常,但是当我调用该函数并传递给定常量之一的参数时, map 上出现的所有图钉都是红色的(默认值
为了在 Storyboard iOS 项目中创建 map 注释,我使用了: CLLocationCoordinate2D annotationCoord3; annotatio
在我的 viewWillAppear 中有一个 View MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; [point set
有没有办法阻止启用 MKMapView 实例中的注释。换句话说,当用户点击 map 上的红色图钉时,有没有办法阻止它突出显示图钉。现在,触摸时引脚会变黑... 编辑:我正在使用以下代码返回 MKPin
我想知道如何更新我的 annotation我拖动它时的标题,因为所有 annotation属性是get仅。 我尝试过的代码: func mapView(_ mapView: MKMapView, an
摘要:使用应用程序中的解析链接(而非查询)注释 已完成:我想做的是让新创建的注释成为 PFObject。我尝试执行此操作的方法是获取基于 MKPointAnnotation 的坐标。然后将其转换为带有
我正在尝试在 MapView 上显示两个注释。 为什么第二个注释不显示在 MapView 中?我尝试将地标[0]更改为放置标记[1],但没有帮助。 我本来可以使用 for 子句,但为了测试我重复了代码
我想知道当我点击标题时如何转到另一个 View MKPointAnnotation func addPin(lat:Double, lng:Double) { let testPin
我是 Xcode 新手,我从未使用 swift 创建过 iphone 应用程序,所以我在这方面仍然遇到很多麻烦。 在我的应用程序中,我想打开 map 页面,并希望为我拥有的每个地址显示一个图钉。为此,
我正在尝试在 MKPointAnnotations 上添加不同的图像。这是我的代码,当只有一个注释时它工作正常。我面临在不同注释上添加不同图像的问题。 func showDevices(Devs: A
当前正在使用 map View 并向 map 添加图钉。我知道如何使用 addAnotation() 方法向 map 添加单个点。现在,我尝试以最简单的方式向 MapView 添加多个点。我已经获取了
我有一个 map 应用程序,允许用户将注释保存为可变数组的收藏夹。然后可以在用户选择时显示所有喜欢的注释。 添加到可变数组的注释属于 MKPointAnnotation 类。我可以正确地向可变数组添加
我是一名优秀的程序员,十分优秀!