gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:59:24 25 4
gpt4 key购买 nike

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

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! ***");
}

}

我正在尝试遵循各种在线指南并熟悉跟踪用户并最终在 map 上显示其他用户。现在,在实时跟踪用户时,我对 CLLocationManager 委托(delegate)与 mapView.setUserTrackingMode 的使用也有点困惑。

我到处寻找解决方案,但在这里完全被难住了。任何帮助是极大的赞赏。

最佳答案

map View 调用将产生临时位置权限。而位置管理器将允许您更严格地指定行为。请务必将适当的行添加到您的 info.plist 中。就你而言;添加下面的中间条目,您的位置管理器应该开始更新。

info.plist

关于ios - CLLocationManager 委托(delegate)不称为 ios 11 (swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47445589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com