Swift 4 Mapview没有调用委托方法f

时间:2018-04-24 20:48:21

标签: swift annotations delegates mapkit

当用户使用(https://www.raywenderlich.com/151817/how-to-make-an-app-like-pokemon-go)的教程从我的位置点击50米的敌人时,我尝试呈现一个视图控制器。但是我的viewcontroller没有显示,我认为我没有从扩展中调用我的委托方法。我如何解决它? 这是我的代码:

import UIKit
import MapKit
class GoogleMapsViewController: UIViewController, CLLocationManagerDelegate {
    var targets = [Item]()
    var locationManager: CLLocationManager!
    var userLocation: CLLocation?
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.showsUserLocation = true
        mapView.mapType = MKMapType(rawValue: 0)!
        mapView.userTrackingMode = MKUserTrackingMode(rawValue: 2)!
        setupLocations()        // Do any additional setup after loading the view.
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        // Check for Location Services

        if CLLocationManager.locationServicesEnabled() {
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
            locationManager.startUpdatingHeading()

        }

    } 

    func setupLocations() {
        let pdpk = Item( Place : "Pildammsparken", location: CLLocation(latitude: 55.590105, longitude:  12.988737))
        targets.append(pdpk)
        let btp = Item( Place : "Bulltoftaparken", location: CLLocation(latitude: 55.601326, longitude:  13.079005))
        targets.append(btp) for item in targets {
            let annotation = MapA(location: item.location.coordinate, item: item)
            self.mapView.addAnnotation(annotation)

        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        defer { userLocation = locations.last }

        if userLocation == nil {
            // Zoom to user location
            if let uLocation = locations.last {
                let viewRegion = MKCoordinateRegionMakeWithDistance(uLocation.coordinate, 2000, 2000)
                mapView.setRegion(viewRegion, animated: false)
                print(uLocation)

            }

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension GoogleMapsViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        self.userLocation = userLocation.location
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        //1
        let coordinate = view.annotation!.coordinate
        //2
        if let userCoordinate = userLocation {
            //3
            if userCoordinate.distance(from: CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)) < 50 {
                //4
                print("test")
                let storyboard = UIStoryboard(name: "Main", bundle: nil)

                if let viewController = storyboard.instantiateViewController(withIdentifier: "ARViewController") as?    PlaceViewController {
                    // more code later
                    //5
                    if let mapAnnotation = view.annotation as? MapA {
                        //6
                        self.present(viewController, animated: true, completion: nil)
                    }
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您必须设置viewDidLoad

mapView.delegate = self

答案 1 :(得分:0)

Sh_Khan是绝对正确的。

您还可以控制拖动到左侧的视图控制器图标,然后选择&#34;委托&#34;从弹出菜单中。您还可以为表,选择器等对象设置数据源。

答案 2 :(得分:0)

您可以通过两种方式设置代理

  1. 通过代码Exception in thread "main" org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: A new session could not be created. (Original error: session not created exception: Chrome version must be >= 60.0.3112.0 (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.14393 x86_64)) (WARNING: The server did not provide any stacktrace information)
  2. 通过界面构建​​器。通过将mapview的委托与viewcontroller连接起来。参考截图已附上
  3. enter image description here