- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 MKMapView 上显示自定义图钉。这些图钉将具有自定义图像以及将显示值的 UILabel。
我能够成功创建带有标签的自定义图钉。现在标签显示静态值。我从像解析这样的后端服务中查询数据,并为每个点保存数据。这样,当用户点击某个点时,我可以在 viewController
中显示数据,但是我不确定如何将这些数据从我的查询方法传递到 didSelectAnnotation 和 viewForAnnotation 方法。
我还想将标签显示的静态值更改为从服务器查询的值。我试图通过创建一个名为 CustomPointAnnotation
的类来实现这一点,该类继承自 MKPointAnnotation
并具有一个具有三个属性的初始化程序。这些属性是在查询期间设置的,因此我如何在 mapViewDidSelectAnnotationView
和 viewForAnnotation
函数中访问这些属性,以便我可以根据需要使用数据。 (例如将 viewController
中标签的文本设置为该特定注释的属性)。
下面是一张显示 viewController 和我目前拥有的图像:
这是自定义点类:
class CustomPointAnnotation: MKPointAnnotation {
var price: String!
var streetName: String!
var ratingValue: Int!
init?(price: String, streetName: String, ratingValue: Int) {
self.price = price
self.streetName = streetName
self.ratingValue = ratingValue
super.init()
}
下面是我在 viewDidLoad 中运行的查询:
func displayPoints() {
let pointsQuery = PFQuery(className: "testLocation")
let currentLocation = PFGeoPoint(location: locationManager.location)
pointsQuery.whereKey("location", nearGeoPoint: currentLocation, withinMiles: 2)
pointsQuery.findObjectsInBackgroundWithBlock { (points, error) -> Void in
if error == nil {
print("number of spots: \(points?.count)")
let spots = points! as [PFObject]
for pinPoint in spots {
let point = pinPoint["location"] as! PFGeoPoint
let price = String(pinPoint["price"])
let ratingValue = pinPoint["rating"] as! Int
let streetName = "Park Street, San Francisco CA"
self.customAnnotation = CustomPointAnnotation(price: price, streetName: streetName, ratingValue: ratingValue)
//// PRINT DATA OBTAINED FOR TESTING PURPOSES///////////////////////////////////////////////////////////
print(self.customAnnotation.price)
print(self.customAnnotation.streetName)
print(self.customAnnotation.ratingValue)
///////////////////////////////////////////////////////////////////////////////////////////////////////
self.customAnnotation!.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
self.priceArray.append(pinPoint["price"])
self.customAnnotation!.price = pinPoint["price"] as? String
self.mapView.addAnnotation(self.customAnnotation!)
}
} else {
JSSAlertView().danger(self, title: "something went wrong", text: "error: \(error)")
}
}
}
这里是 didSelectAnnotationView:
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
//var anot: MKAnnotation
if ((view.annotation?.isKindOfClass(MKUserLocation)) != nil){
view.image = nil
}
for anot in mapView.annotations {
print(mapView.annotations.count)
let annotationView = mapView.viewForAnnotation(anot)
if (annotationView != nil) {
annotationView?.image = UIImage(named: "pin")
priceLabel.textColor = UIColor.whiteColor()
}
//priceLabel.textColor = UIColor.blueColor()
view.image = UIImage(named: "pinselected")
print("image changed")
}
}
最后是 viewForAnnotation 方法:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKindOfClass(MKUserLocation){
return nil
}
if !(annotation is CustomPointAnnotation) {
print("all custom images added")
return nil
}
let reuseID = "identifier"
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID)
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: reuseID, price: "13" )
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
//let cpa = annotation as! CustomPointAnnotation
//let annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: nil, price: "11")
//annotationView!.addSubview(priceLabel)
annotationView?.annotation = annotation
annotationView?.image = UIImage(named: "pin.png")
return annotationView
}
最佳答案
你可以使用 as 运算符在 swift 中进行向下转换。在 didSelectAnnotationView 中,annotationView 有一个注释属性。您的自定义注释 View 会将您的自定义注释作为其注释属性,因此您可以通过以下方式尝试将其向下转换为您的子类:
if let annotation = view.annotation as? CustomPointAnnotation
假设这是可能的,那么您将可以访问子类的属性。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
//var anot: MKAnnotation
if ((view.annotation?.isKindOfClass(MKUserLocation)) != nil){
view.image = nil
}
for anot in mapView.annotations {
print(mapView.annotations.count)
let annotationView = mapView.viewForAnnotation(anot)
if (annotationView != nil) {
annotationView?.image = UIImage(named: "pin")
priceLabel.textColor = UIColor.whiteColor()
}
//priceLabel.textColor = UIColor.blueColor()
}
view.image = UIImage(named: "pinselected")
if let annotation = view.annotation as? CustomPointAnnotation
{
self.priceLabel.text = annotation.price //for example
//update the rest of your UI
}
print("image changed")
}
与 viewForAnnotation 类似,您可以将 MKAnnotation 向下转换为 CustomPointAnnotation,将 MKAnnotationView 向下转换为 CustomAnnotationView。
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKindOfClass(MKUserLocation){
return nil
}
if !(annotation is CustomPointAnnotation) {
print("all custom images added")
return nil
}
let reuseID = "identifier"
let cpa = annotation as! CustomPointAnnotation
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as! CustomAnnotationView
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: cpa, reuseIdentifier: reuseID, price: cpa.price)
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = cpa
annotationView?.price = cpa.price
}
annotationView?.image = UIImage(named: "pin.png")
return annotationView
}
当通过实现价格的 didSet 设置价格时,您的 CustomAnnotationView 应该更新其价格标签。
关于ios - 将数据从查询传递到 mapView viewForAnnotation 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009522/
我正在创建自定义注释,并且正在尝试使用 dequeueReusableAnnotation。 引脚之间的区别在于用于引脚图像的 png。 我创建了 myAnnotation 类,并在创建注释时使用了此
扫描了所有类似的问题并尝试了所有可能的解决方案后,我仍然找不到适合我的情况的解决方案。我尝试在我的 MapView 上放置多个图钉,但似乎无法正常添加我的图钉。我在 viewDidLoad 方法中启动
在我的 viewForAnnotation 方法中,我在图钉右侧添加了一个详细信息披露按钮,但它会将 showsUserLocation 更改为红色图钉,并且在“当前位置”文本右侧有一个披露按钮。 如
我正在 map 上显示图钉,但无法自定义注释 View 的显示。出于某种原因,我的 viewForAnnotation 没有被调用。这是 didFinishLaunchingWithOptions 方
我已经添加UIViewController在 .h 中并已添加 -(void) viewDidLoad { self.mapView.delegate = self; } 但是 view
我已经实现了 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {方法来更改 us
我正在尝试制作自定义注释。我制作了一个自定义注释类,我在其中设计了注释 View 。我试图在我的 Viewcontroller 中调用 viewForAnnotation 中的类。当我运行该应用程序时
最近我在我的应用程序中实现了 viewForAnnotation 方法,以使用我的注释的特征(图像、颜色等)。唯一的问题是,虽然该功能在我的代码中,但定位服务的行为很奇怪。蓝色位置标记不是“脉动”并且
我的 viewForAnnotation 在 iOS8 中运行良好,但在 iOS9 中不显示自定义图钉图像。图片路径没问题。 怎么了 ?谢谢! func mapView(mapView: MKMapV
在我花了 2 天时间搜索错误后,我不得不在这里寻求帮助。我有 MapViewController 并在 map 上放置了一些图钉。我从苹果代码示例的 MapCallouts 和 WeatherMap
我添加了这个: func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
目标是根据存储在结构数组中的某些值自定义引脚颜色。 根据此处的一些帮助,我实现了以下 viewForAnnotation 委托(delegate)方法,并且根据我的结构数据数组的大小在循环中迭代调用此
我正在尝试制作一张 map ,在那里我可以看到我当前的位置,并看到这条街叫什么。 到目前为止,我可以在我的 map 上放置图钉,但出于某种原因,我没有收到标注。并且我在我的 viewForAnnota
我知道这个问题之前已经被其他人问过,并且我之前已经在这个论坛上阅读过它们,但是到目前为止我已经尝试了所有建议的方法,但没有运气,所以我决定发布这个问题。 我在下面有这段代码,用于更改 MKMapVie
我将一个完美运行的 6.1 应用程序升级到了 Xcode 5/ios 7使用 MKMapKit 在 map View 上显示一些图钉。 现在注释图钉不显示了。我确定我的 mapview 委托(dele
我想创建基本的个人资料图像注释而不是默认注释图钉,但我不知道在这种情况下如何使用 viewForAnnotation。 有没有办法创建类似 tableView 页眉页脚 View 的 xib 文件?或
当我没有在 MKMapView 上设置委托(delegate)而是添加注释时,它们的图像如下所示: 但是,如果我像这样返回一个 MKAnnotationView: func mapView(_ map
我正在尝试在 MKMapView 上显示自定义图钉。这些图钉将具有自定义图像以及将显示值的 UILabel。 我能够成功创建带有标签的自定义图钉。现在标签显示静态值。我从像解析这样的后端服务中查询数据
我有不同的类(MKAnnotation 子类)代表 map 注释。 我将它们加载到 map 上,一切正常,但是当我移动或缩放 map 图钉时,它们的图像开始丢失。 当我平移 map viewForAn
我看过其他关于此的报告,但似乎没有一个适用。我们的应用程序因频繁(但无法可靠地重现)崩溃而瘫痪,如下所示: Thread 0 name: Dispatch queue: com.apple.main
我是一名优秀的程序员,十分优秀!