【问题标题】:Open Map AnnotationView from selectedRow in tableView从 tableView 中的选定行打开地图注释视图
【发布时间】:2018-05-10 17:17:30
【问题描述】:

我如何在选择 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


        } 

这是我的地图 didselect 功能。

 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)


    }

【问题讨论】:

  • mapView.selectAnnotation 仅在您传递已添加到 mapview 中的注释对象时有效
  • 好吧,我的对象已经全部添加到地图中,当我按下单元格时,它以相关注释为中心,我想要的只是,另外要弹出标注视图,就像我选择了它一样....

标签: ios swift uitableview mapkit mkannotationview


【解决方案1】:

在您的didSelectRowAtIndexPath, 你传递的是customXibView 而不是那个

您必须搜索与该 tableview 关联的 Annotation

一旦你找到它,你可以告诉它,通过使用来选择它:

mapView.selectAnnotation

您可以使用https://stackoverflow.com/a/39980302/4601900 答案从MapView 中搜索注释

【讨论】:

    【解决方案2】:
    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
    
    
            } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 2021-06-15
      相关资源
      最近更新 更多