Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 4 Mapview未调用委托方法f_Swift_Annotations_Delegates_Mapkit - Fatal编程技术网

Swift 4 Mapview未调用委托方法f

Swift 4 Mapview未调用委托方法f,swift,annotations,delegates,mapkit,Swift,Annotations,Delegates,Mapkit,当用户点击距离我位置50米的敌人时,我尝试使用教程from()呈现一个视图控制器。但我的viewcontroller并没有显示,我想我并没有从扩展调用我的委托方法。我怎么修理它? 这是我的代码: import UIKit import MapKit class GoogleMapsViewController: UIViewController, CLLocationManagerDelegate { var targets = [Item]() var locationMana

当用户点击距离我位置50米的敌人时,我尝试使用教程from()呈现一个视图控制器。但我的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)
                    }
                }
            }
        }
    }
}
导入UIKit
导入地图套件
类GoogleMapsViewController:UIViewController、CLLocationManagerDelegate{
变量目标=[项目]()
var locationManager:CLLocationManager!
var userLocation:CLLocation?
@ibvar映射视图:MKMapView!
重写func viewDidLoad(){
super.viewDidLoad()
mapView.showsUserLocation=true
mapView.mapType=MKMapType(原始值:0)!
mapView.userTrackingMode=MKUserTrackingMode(原始值:2)!
setupLocations()//加载视图后执行任何其他设置。
locationManager=CLLocationManager()
locationManager.delegate=self
locationManager.desiredAccuracy=KCallocationAccuracyBest
//查看定位服务
如果CLLocationManager.locationServicesEnabled(){
locationManager.RequestWhenUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
}
} 
func setupLocations(){
设pdpk=项目(地点:“Pildammsparken”,地点:CLLocation(纬度:55.590105,经度:12.988737))
targets.append(pdpk)
设btp=项目(地点:“Bulltoftaparken”,地点:CLLocation(纬度:55.601326,经度:13.079005))
目标。为目标中的项目追加(btp){
let annotation=MapA(位置:item.location.coordinate,item:item)
self.mapView.addAnnotation(注释)
}
}
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
延迟{userLocation=locations.last}
如果userLocation==nil{
//缩放到用户位置
如果let uLocation=locations.last{
让viewRegion=MKCoordinateRegionMakeWithDistance(uLocation.coordinate,2000,2000)
mapView.setRegion(viewRegion,动画:false)
印刷品(位置)
}
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
}
扩展GoogleMapsViewController:MKMapViewDelegate{
func映射视图(uMapView:MKMapView,didUpdate用户位置:MKUserLocation){
self.userLocation=userLocation.location
}
func映射视图(uMapView:MKMapView,didSelect视图:MKAnnotationView){
//1
让坐标=视图。注释!。坐标
//2
如果让userCoordinate=userLocation{
//3
如果userCoordinate.distance(from:CLLocation(纬度:坐标.纬度,经度:坐标.经度))<50{
//4
打印(“测试”)
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
如果让viewController=storyboard.instanceeviewcontroller(标识符为“ARViewController”)作为?PlaceViewController{
//稍后会有更多代码
//5
如果让mapAnnotation=view.annotation为?MapA{
//6
self.present(viewController,动画:true,完成:nil)
}
}
}
}
}
}

您必须在
viewDidLoad

mapView.delegate = self

舒可汗是绝对正确的


您还可以控制拖动到左侧的视图控制器图标,并从弹出菜单中选择“委派”。您还可以为表、选择器等对象设置数据源。

您可以通过两种方式设置委托

  • 通过代码mapview.delegate=self
  • 通过界面生成器。通过将mapview的代理与viewcontroller连接。附参考截图

  • 这起作用了!我在其他地方看过,这到底是做什么的?