从 tableView 中的 selectedRow 打开 Map AnnotationView
Open Map AnnotationView from selectedRow in tableView
如何在 select table 查看单元格后将 MKAnnotationView 放置在指定的注释上?我是否需要在 table 视图的 didSelectRowAtIndexPath 中调用函数 mapView(_:didSelect:) ?这是我的 didselectrow 函数。我确实在 select 行中尝试使用 map.selectAnnotation,但是 returns 没有。
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
}
这是我的地图select func.
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)
}
在你的didSelectRowAtIndexPath
中,
您传递的是 customXibView
而不是
您必须搜索与该表视图关联的注释
找到它后,您可以告诉它 select 它,方法是:
mapView.selectAnnotation
您可以使用 答案从 MapView
中搜索注释
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(filteredDetails[indexPath.row], animated: true)
tableView.isHidden = true
}
如何在 select table 查看单元格后将 MKAnnotationView 放置在指定的注释上?我是否需要在 table 视图的 didSelectRowAtIndexPath 中调用函数 mapView(_:didSelect:) ?这是我的 didselectrow 函数。我确实在 select 行中尝试使用 map.selectAnnotation,但是 returns 没有。
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
}
这是我的地图select func.
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)
}
在你的didSelectRowAtIndexPath
中,
您传递的是 customXibView
而不是
您必须搜索与该表视图关联的注释
找到它后,您可以告诉它 select 它,方法是:
mapView.selectAnnotation
您可以使用 MapView
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(filteredDetails[indexPath.row], animated: true)
tableView.isHidden = true
}