在选择tableview单元格后,如何将MKAnnotationView放置在指定的注释上?我是否需要在表视图的didSelectRowAtIndexPath中调用函数mapView(_:didSelect :)?这是我的didselectrow功能。我确实尝试过didselectrow中的map.selectAnnotation,但返回nil。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let pin = filteredDetails[indexPath.row].coordinate
let latDelta: CLLocationDegrees = 0.009
let lonDelta: CLLocationDegrees = 0.009
let span = MKCoordinateSpanMake(latDelta, lonDelta)
let location = CLLocationCoordinate2DMake(pin.latitude, pin.longitude)
let region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
// mapView.selectAnnotation(customXibView as! MKAnnotation, animated: true)
tableView.isHidden = true
}
这是我的地图选择功能。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let meridianAnnotation = view.annotation as! Details
let customXibView = Bundle.main.loadNibNamed("CustomCalloutVIew", owner: self, options: nil)?[0] as! CustomAnnotationClass!
customXibView?.address.text = meridianAnnotation.address
customXibView?.phone.text = meridianAnnotation.phone
let calloutFrame = view.frame
customXibView?.frame = CGRect(x: (calloutFrame.size.width)-178.0, y: (calloutFrame.size.height)-220, width: 315, height: 166)
customXibView?.blurOverlay.effect = UIBlurEffect(style: .light)
customXibView?.layer.masksToBounds = true
let shadowView = UIView(frame: CGRect(x: (calloutFrame.size.width)-178.0, y: (calloutFrame.size.height)-220, width: 315, height: 154))
shadowView.translatesAutoresizingMaskIntoConstraints = false
shadowView.backgroundColor = UIColor.clear
shadowView.layer.borderWidth = 7.0
shadowView.layer.borderColor = UIColor.lightGray.cgColor
shadowView.alpha = 1.0
shadowView.layer.cornerRadius = 10.0
shadowView.layer.shadowOffset = CGSize(width: 4.0, height: 4.0)
shadowView.layer.shadowOpacity = 1.0
shadowView.layer.shadowRadius = 13.0
view.insertSubview(shadowView, at: 0)
view.addSubview(customXibView!)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}