大家好,我是iOS编程的新手,我一直在努力解决这个问题。未调用我的locationManager委托。这是我的代码:

import UIKit;
import MapKit;

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

    var mapView: MKMapView!;

    let locationManager = CLLocationManager();

    override func loadView() {
        // Create a map view
        mapView = MKMapView();

        // Set it as *the* view of this view controller
        view = mapView

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print("MapViewController loaded its view.");
        mapView.showsUserLocation = true;

        let btnImage = UIImage(named: "mapview-track-user.png");

        //Tracking button creation code
        let trackButton = UIButton(type: UIButtonType.custom) as UIButton;
        trackButton.frame = CGRect(x: 10, y: 500, width: 60, height: 60);
        trackButton.setImage(btnImage, for: UIControlState.normal);
        trackButton.addTarget(self, action: #selector(centerMapOnUserButtonClicked), for: .touchDown);

        //Shadow for track user location button
        trackButton.layer.shadowColor = UIColor.lightGray.cgColor;
        trackButton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0);
        trackButton.layer.masksToBounds = false;
        trackButton.layer.shadowRadius = 1.0;
        trackButton.layer.shadowOpacity = 0.9;

        mapView.addSubview(trackButton);
        locationManager.delegate = self;
        mapView.delegate = self;
        locationManager.requestAlwaysAuthorization();
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        if CLLocationManager.locationServicesEnabled() {
            print("Location servicese are enabled!");
            locationManager.startUpdatingLocation();
            locationManager.startUpdatingHeading();
        }
        else {
            print("Location Services not endabled!");
        }
    }

    //Called upon clicking track user button in order to snap map to user
    @objc func centerMapOnUserButtonClicked() {
        self.mapView.setUserTrackingMode(MKUserTrackingMode.followWithHeading, animated: true);
        print("Track button clicked!");
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!) {
        let location = locations.last as! CLLocation
        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        var region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
        region.center = mapView.userLocation.coordinate;
        mapView.setRegion(region, animated: true)
        print("*** locationManager Called! ***");
    }

}


我正在尝试在线遵循各种指南,并熟悉跟踪用户并最终在地图上显示其他用户。现在,当涉及实时跟踪用户时,对于CLLocationManager委托与mapView.setUserTrackingMode的使用,我也有些困惑。

我到处都在寻找解决方案,但在这里完全陷入了困境。任何帮助是极大的赞赏。

最佳答案

地图视图调用将产生对位置的临时许可。而位置管理器将允许您更严格地指定行为。请确保在您的info.plist中添加适当的行。就你而言从下面添加中间条目,您的位置经理应开始更新。

ios - CLLocationManager委托(delegate)未称为ios 11(swift)-LMLPHP

10-08 16:01