0
votes

How can I place MKAnnotationView on specified annotation after I select tableview cell? Do I somehow need to call function mapView(_:didSelect:) within didSelectRowAtIndexPath of table view? Here is my didselectrow func. I did try with map.selectAnnotation within didselectrow, but returns 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


        } 

And this is my map didselect 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)


    }
2
mapView.selectAnnotation only works when you passed annotation object which is already added in mapview - Prashant Tukadiya
well, my objects are already all added to the map, when I press on cell It centers map on related annotation, all I want is, additionally for callout view to pop-up as if I selected it.... - NikolaS

2 Answers

0
votes

In your didSelectRowAtIndexPath, You are passing customXibView instead of that

You have to search for Annotation associated with that tableview

Once you find it, you can tell it, to select it by using:

mapView.selectAnnotation

You can use https://stackoverflow.com/a/39980302/4601900 answer to search annotation from MapView

0
votes
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


        }