2017-07-01 79 views
0

我正试图在地图上显示针脚,并带有预设的搜索参数。点击按钮后,该应用会显示所有本地出租车服务。这部分我有...我有项目追加到[MKPointAnnotation],它成功地在tableView以及mapView上显示一个填充列表。出于某种原因,当您点击mapView上的引脚时,即使执行viewFor annotation:委托,我也无法获得标题和副标题。有任何想法吗?Swift MKMapView针脚注释问题

class MapViewController: UIViewController, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate, MKMapViewDelegate { 

@IBOutlet weak var mapView: MKMapView! 
@IBOutlet weak var tableView: UITableView! 

let locationManager = CLLocationManager() 
let regionRadius: CLLocationDistance = 1500 
var matchingItems: [MKMapItem] = [MKMapItem]() 
var annotationGroup = [MKPointAnnotation]() 

override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     tableView.delegate = self 
     tableView.dataSource = self 
     mapView.delegate = self 


     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
     tableView.allowsSelection = true 


     let currentLocation = locationManager.location 
     guard let latitude = currentLocation?.coordinate.latitude, 
      let longitude = currentLocation?.coordinate.longitude else { 
       alertPopup(title: "Enable Location Services", message: "Navigate to Settings >", buttonTitle: "Ok") 
       return 
     } 
     let initialLocation = CLLocation(latitude: latitude, longitude: longitude) 
     tabBarController?.tabBar.isHidden = true 
     centerMapOnLocation(location: initialLocation) 

     let request = MKLocalSearchRequest() 
     request.naturalLanguageQuery = "Taxi" 
     request.region = mapView.region 

     let search = MKLocalSearch(request: request) 

     search.start(completionHandler: {(response, error) in 

      if error != nil { 
       print("Error occured in search: \(error!.localizedDescription)") 
      } else if response!.mapItems.count == 0 { 
       print("No Matches Found") 
      } else { 
       print("Matches Found") 
      } 

      for location in response!.mapItems { 
       print("Name = \(String(describing: item.name))") 
       print("Phone = \(String(describing: item.phoneNumber))") 
       self.matchingItems.append(item) 

       var pinAnnotationView = MKPinAnnotationView() 
       let annotation = MKPointAnnotation() 
       annotation.coordinate.longitude = location.placemark.coordinate.longitude 
       annotation.coordinate.latitude = location.placemark.coordinate.latitude 
       annotation.title? = location.name! 
       annotation.subtitle = location.phoneNumber! 
       self.annotationGroup.append(annotation) 
       self.mapView.addAnnotations(self.annotationGroup) 
       pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil) 
       self.mapView.addAnnotation(pinAnnotationView.annotation!) 
      } 
      self.tableView.reloadData() 
      print("Reloaded Table Data") 
      print(self.matchingItems) 
      self.mapView.showAnnotations(self.annotationGroup, animated: true) 

     }) 
    } 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 

     if !(annotation is MKUserLocation) { 
      let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash)) 

      let rightButton = UIButton(type: .contactAdd) 
      rightButton.tag = annotation.hash 

      pinView.animatesDrop = true 
      pinView.canShowCallout = true 
      pinView.rightCalloutAccessoryView = rightButton 

      return pinView 
     } else { 
      print("Returned nil") 
      return nil 
     } 

    } 
} 
+0

我认为问题是,你是不是直接添加注释self.mapView。相反,您将其添加到self.annotationGroup。你不解释它是什么。 –

+0

@El Tomato'annotationGroup'是在MapViewController类的开头声明的注释数组,我将注释附加到循环中。我将注释数组添加到'mapView' ..'self.mapView.addAnnotations(self.annotationGroup')。 – szady

回答

0

问题是可选的annotation.title? = location.name!。删除可选,它的作品。

for location in response!.mapItems { 
     print("Name = \(String(describing: location.name))") 
     print("Phone = \(String(describing: location.phoneNumber))") 
     self.matchingItems.append(location) 
     let annotation = MKPointAnnotation() 
     annotation.coordinate.longitude = location.placemark.coordinate.longitude 
     annotation.coordinate.latitude = location.placemark.coordinate.latitude 
     annotation.title = location.name! //This is the line to remove the optional annotation.title? from. 
     annotation.subtitle = location.phoneNumber! 
     self.annotationGroup.append(annotation) 
     self.mapView.addAnnotations(self.annotationGroup) 
     self.mapView.showAnnotations(self.annotationGroup, animated: true) 
     } 
0

也许为时已晚,但无论如何

首先,MKAnnotation是一个协议,所以我认为你必须定义一个自定义类的对象,这个类必须实现这个协议或者在这种情况下,我们可以定义MKMapItem一个扩展实施MKAnnotation协议

extension MKMapItem : MKAnnotation 
{ 
    public var coordinate: CLLocationCoordinate2D { 
    return self.placemark.coordinate 
    } 


    // Title and subtitle for use by selection UI. 
    public var title: String? { 
    return self.name 
    } 

    public var subtitle: String? { 
    return self.phoneNumber 
    } 
} 

这个代码将减少到这

search.start(completionHandler: {(response, error) in 

      if error != nil { 
       print("Error occured in search: \(error!.localizedDescription)") 
      } else if response!.mapItems.count == 0 { 
       print("No Matches Found") 
      } else { 
       print("Matches Found") 
      } 

      for location in response!.mapItems { 
       print("Name = \(String(describing: item.name))") 
       print("Phone = \(String(describing: item.phoneNumber))") 
       self.matchingItems.append(item) 
      } 
      self.mapView.addAnnotations(self.matchingItems) 
      self.tableView.reloadData() 
      print("Reloaded Table Data") 
      print(self.matchingItems) 
      self.mapView.showAnnotations(self.matchingItems, animated: true) 
     }) 

一旦你有了这个那么你的func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}实施应该工作

希望这有助于