“MKMapItem”类型的值在 Swift 3 中没有成员“网站”

Posted

技术标签:

【中文标题】“MKMapItem”类型的值在 Swift 3 中没有成员“网站”【英文标题】:Value of type 'MKMapItem' has no member 'website` in Swift 3 【发布时间】:2017-11-09 21:26:35 【问题描述】:

我正在创建一个应用程序,其中有注释视图,当您单击注释视图时,它不会在视图控制器DetailsView 上显示注释视图网站网址,请查看我的代码并帮助我解决它显示注释视图位置的网站 URL。

这是我的代码:

import UIKit
import MapKit

protocol UserLocationDelegate 
    func userLocation(latitude: Double, longitude: Double)


class NearMeMapViewController: ARViewController, ARDataSource, MKMapViewDelegate, CLLocationManagerDelegate 

    var nearMeIndexSelected = NearMeIndexTitle()
    var locationManager: CLLocationManager!
    var nearMeARAnnotations = [ARAnnotation]()

    var nearMeRequests = [NearMeRequest]()
    var delegate: UserLocationDelegate!

    var place: Place?

    override func viewDidLoad() 
        super.viewDidLoad()

        self.title = nearMeIndexSelected.indexTitle

        self.locationManager = CLLocationManager()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.distanceFilter = kCLHeadingFilterNone
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()

        self.dataSource = self
        self.headingSmoothingFactor = 0.05
        self.maxVisibleAnnotations = 30

        getNearMeIndexSelectedLocation()
    

    func getNearMeIndexSelectedLocation() 

        let nearMeRequest = MKLocalSearchRequest()
        nearMeRequest.naturalLanguageQuery = nearMeIndexSelected.indexTitle

        let nearMeregion = MKCoordinateRegionMakeWithDistance(self.locationManager.location!.coordinate, 250, 250)
        nearMeRequest.region = nearMeregion

        let nearMeSearch = MKLocalSearch(request: nearMeRequest)

        nearMeSearch.start(response: MKLocalSearchResponse?, error: Error?) in

            for requestItem in (response?.mapItems)! 

                let nearMeIndexRequest = NearMeRequest()
                nearMeIndexRequest.name = requestItem.name
                nearMeIndexRequest.coordinate = requestItem.placemark.coordinate
                nearMeIndexRequest.address = requestItem.placemark.addressDictionary?["FormattedAddressLines"] as! [String]
                nearMeIndexRequest.street = requestItem.placemark.addressDictionary?["Street"] as! String!
                nearMeIndexRequest.city = requestItem.placemark.addressDictionary?["City"] as! String
                nearMeIndexRequest.state = requestItem.placemark.addressDictionary?["State"] as! String
                nearMeIndexRequest.zip = requestItem.placemark.addressDictionary?["ZIP"] as! String
                nearMeIndexRequest.phone = requestItem.phoneNumber
                nearMeIndexRequest.website = requestItem.website // This is where the error is at.

                self.nearMeRequests.append(nearMeIndexRequest)
                print(requestItem.placemark.name)
            

            for nearMe in self.nearMeRequests 
                let annotation = NearMeAnnotation(nearMeRequest: nearMe)
                self.nearMeARAnnotations.append(annotation)
                self.setAnnotations(self.nearMeARAnnotations)
            
        
    

    func ar(_ arViewController: ARViewController, viewForAnnotation: ARAnnotation) -> ARAnnotationView 

        let annotationView = NearMeARAnnotationView(annotation: viewForAnnotation)
        annotationView.frame = CGRect(x: 0, y: 0, width: 150, height: 50)

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
        annotationView.addGestureRecognizer(tapGesture)

        return annotationView
    

    func tapBlurButton(_ sender: UITapGestureRecognizer) 

        if let annotationView = sender.view as? NearMeARAnnotationView 

            if let detailsVc = storyboard?.instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController 

                detailsVc.annotation = annotationView.annotation
                detailsVc.place = Place(location: (locationManager.location)!,
                                        reference: "",
                                        name: annotationView.annotationNameLabel.text ?? "",
                                        address: annotationView.annotationAddressLabel.text ?? "",
                                        phoneNumber: annotationView.phoneNumber.text ?? "",
                                        website: annotationView.website.text ?? "")

                self.navigationController?.pushViewController(detailsVc, animated: true)
            
        
    


【问题讨论】:

【参考方案1】:

MKMapItem 上没有网站资源。有一个 url 属性,但是它应该包含与该位置关联的网站。

这是 Apple 的文档对地图项的 url 属性的评价。

如果存在与该位置关联的相关 URL,例如该位置的企业 URL,请使用此属性指定该值。

你的代码行:

nearMeIndexRequest.website = requestItem.website

应该改为:

nearMeIndexRequest.website = requestItem.url.absoluteString

【讨论】:

我尝试通过url 属性来实现。但是,我被卡住了,所以我就这样做了,这就是为什么我在这里为你们提供帮助。你能告诉我用url属性正确实现它的正确方法吗? 另外,我不想设置硬编码值。我想像我对 phoneNumber 所做的那样做......每当用户点击注释视图时,它都会显示将网站 url 放置在下一个视图控制器上。 您想要网站的 URL 还是字符串? 然后我将它从 var website :String! 更改为 var website: Url? 但它仍然不起作用.. 我要那个地方网站的网址。

以上是关于“MKMapItem”类型的值在 Swift 3 中没有成员“网站”的主要内容,如果未能解决你的问题,请参考以下文章

“NSMutableDictionary”类型的值在 swift 中没有成员“字符串”

在 Swift 中将 CLLocation 转换为 MKMapItem

MKPointAnnotation 到 MKMapItem

swift-Array(数组)

加或减按钮的值在swift中不更新。

UiPickerview 值在 swift 中显示为问号(“?”) - 但是在将其分配给文本框时获得正确的值