iOS:地图视图注释未显示针脚

Posted

技术标签:

【中文标题】iOS:地图视图注释未显示针脚【英文标题】:iOS: MapView Annotations not showing for pins 【发布时间】:2015-11-20 03:02:11 【问题描述】:

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

这是我的代码:

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
        
    



【问题讨论】:

【参考方案1】:

这似乎是因为您的标题和其他人的副标题是空白的。我测试了引号中没有任何内容并添加了一些内容,这似乎解决了这个问题。

【讨论】:

哇,我不敢相信是-_- 谢谢! @Fumbles 似乎在向地图添加注释时,.title 必须至少有一个字符,即使它是“”。否则,即使 .title 随后被修改为非 "",标签也不会显示。【参考方案2】:

对我来说,在指定我的自定义 mapView.mapType 之后,我还必须告诉 mapView “showAnnotations”。

除非我告诉它显示它们,否则注释不会显示。

    mapView.mapType = mapTypes[Preference.mapType]
    mapView.showAnnotations(mapView.annotations, animated: true) //this fixed it

【讨论】:

以上是关于iOS:地图视图注释未显示针脚的主要内容,如果未能解决你的问题,请参考以下文章

MapKit 地图 - 注释未显示在模拟器上

Flutter 多自定义图标未显示在地图视图上

ios 6.1 注释视图,如标准地图

如何将 UIPopoverView 显示为地图视图的注释? (iPad)

点击集合视图单元以向地图添加注释

React Native mapview:标记未在IOS上显示