【发布时间】:2017-07-01 19:23:04
【问题描述】:
我正在尝试使用预设搜索参数在地图上显示图钉。单击按钮,该应用程序将显示所有本地出租车服务。那部分我记下来了……我有附加到[MKPointAnnotation] 的项目,它成功地在tableView 中显示了一个填充列表以及mapView 上的引脚。出于某种原因,当您点击mapView 上的图钉时,我无法显示标题和副标题,即使在实现viewFor annotation: 委托时也是如此。有什么想法吗?
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
}
}
}
【问题讨论】:
-
我认为问题在于您没有直接向self.mapView添加注释。相反,您将其添加到 self.annotationGroup。你没有解释它是什么。
-
@El Tomato
annotationGroup是在MapViewController类开头声明的注释数组,我将注释附加到循环中。我将注释数组添加到mapView..self.mapView.addAnnotations(self.annotationGroup.
标签: ios swift mkmapview mapkit mapkitannotation