iOS:未显示图钉的MapView注释 [英] iOS: MapView Annotations not showing for pins

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

问题描述

出于某种奇怪的原因,viewForAnnotation仅适用于在viewDidLoad中设置的引脚(这是一个测试引脚).按下时,不会注释在其他位置加载的引脚.我已经设置了代表.我认为这与mapView调用中的标识符有关?但是我不确定如何解决它.任何帮助表示赞赏!谢谢!

For some odd reason the viewForAnnotation is only working for the pin that is set in viewDidLoad (this is a test pin). The pins that are loaded elsewhere aren't getting annotated when pressed. I've already set the delegate. I think it has something to do with the identifier in the mapView call? But I'm unsure of how to fix it. Any help is appreciated! Thanks!

这是我的代码:

import Foundation
import UIKit
import MapKit
import CoreLocation
import Alamofire

class MapViewController: UIViewController, MKMapViewDelegate {

var locationManager:CLLocationManager = CLLocationManager()


@IBOutlet weak var potholeMapView: MKMapView!

var listData: Array<String> = []
var idData: Array<Int> = []
var descriptionData: Array<String> = []

var latitudeData:Array<Double> = []
var longitudeData:Array<Double> = []

override func viewDidLoad() {
    super.viewDidLoad()
    potholeMapView.delegate = self
    locationManager.requestWhenInUseAuthorization()
    potholeMapView!.region = sanDiegoCountyLocation()
    potholeMapView!.mapType = MKMapType.Standard
    potholeMapView!.showsUserLocation = true
    potholeMapView!.showsTraffic = true
    print(potholeMapView!.userLocationVisible)

    // WORKING HERE ACCESSORY VIEW SHOWS
    let encinitas = CLLocationCoordinate2DMake(32.955, -117.2459)
    let marker = AnnotatedLocation(
        coordinate: encinitas,
        title: "There",
        subtitle: "You are not here")
    potholeMapView!.addAnnotation(marker)

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    //HERE ACCESSORY VIEWS DONT SHOW
    loadPotholeData()
}


func sanDiegoCountyLocation()-> MKCoordinateRegion {
    let center = CLLocationCoordinate2DMake(32.76572795, -117.07319880 )
    let widthMeters:CLLocationDistance = 100
    let heightMeters:CLLocationDistance = 1000*120
    return MKCoordinateRegionMakeWithDistance(center, widthMeters, heightMeters)
}

func loadPotholeData(){
    let url = "http://bismarck.sdsu.edu/city/fromDate"
    let parametersGet = ["type" : "street", "user" : "008812"]
    Alamofire.request(.GET, url, parameters: parametersGet)
        .responseJSON { response in
            if let dataGet = response.result.value {
                let dataDict:NSArray = dataGet as! NSArray
                for item in dataDict{
                    let descrip = item["created"]
                    self.listData.append(descrip!! as! String)
                    let ids = item["id"]
                    self.idData.append(ids! as! Int)
                    let description = item["description"]
                    self.descriptionData.append(description!! as! String)
                    let latitude = item["latitude"]
                    self.latitudeData.append(latitude as! Double)
                    let longitude = item["longitude"]
                    self.longitudeData.append(longitude as! Double)
                }
            }
            else {
                print("There was some error getting data")
            }
    }
    createAllPins()
}

func createAllPins(){
    for (x, y) in zip(self.latitudeData, self.longitudeData) {
        let location = CLLocationCoordinate2DMake(x, y)
        let marker = AnnotatedLocation(
            coordinate: location,
            title: "",
            subtitle: "")
        potholeMapView!.addAnnotation(marker)
    }
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? AnnotatedLocation {
        let identifier = "pin"
        var view: MKPinAnnotationView
        if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
            as? MKPinAnnotationView {
                dequeuedView.annotation = annotation
                view = dequeuedView
        } else {
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            //view = MKPinAnnotationView(annotation: <#T##MKAnnotation?#>, reuseIdentifier: <#T##String?#>)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        }
        return view
    }
    return nil
}


func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    self.performSegueWithIdentifier("pushAnnotation", sender: view)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
            case "pushAnnotation":
                let nextVC = segue.destinationViewController as! MapAnnotationDetailViewController
                //nextVC.
            default: break
        }
    }
}

}

推荐答案

似乎是因为您的其他标题和副标题为空白.我进行了测试,没有引号,没有添加任何东西,似乎可以解决该问题.

It appears that it is because your title and subtitle for the others are blank. I tested out having nothing in quotations and adding something, and that seems to fix that issue.

这篇关于iOS:未显示图钉的MapView注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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