类型' MKMapItem的价值'在Swift 3中没有会员'网站`

时间:2017-11-09 21:26:36

标签: ios swift mkmapitem

我正在创建一个应用程序,其中我有注释视图,当您单击注释视图时,它不会在视图控制器DetailsView上显示注释视图网站URL请查看我的代码并帮我解决它通过显示注释视图的网站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 个答案:

答案 0 :(得分:0)

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

以下是Apple的文档对地图项url属性的说法。

  

如果存在与该位置关联的相关网址,例如该位置的商家网址,请使用此属性指定该值。

您的代码行:

nearMeIndexRequest.website = requestItem.website

应更改为:

nearMeIndexRequest.website = requestItem.url.absoluteString