Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Ios 位置跟踪器图标未显示在地图上_Ios_Swift_Mapkit_Cllocationmanager - Fatal编程技术网

Ios 位置跟踪器图标未显示在地图上

Ios 位置跟踪器图标未显示在地图上,ios,swift,mapkit,cllocationmanager,Ios,Swift,Mapkit,Cllocationmanager,目前,我正在设置位置服务,但由于一些奇怪的原因,跟踪器图标没有出现: 我知道它正在工作,因为如果我添加pin,pin将正确显示: 我不知道为什么追踪器没有出现,下面是代码: @IBOutlet weak var map: MKMapView! let locationsManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() locationsManager.d

目前,我正在设置位置服务,但由于一些奇怪的原因,跟踪器图标没有出现:

我知道它正在工作,因为如果我添加pin,pin将正确显示:

我不知道为什么追踪器没有出现,下面是代码:

 @IBOutlet weak var map: MKMapView!

  let locationsManager = CLLocationManager()

  override func viewDidLoad() {
    super.viewDidLoad() 

  locationsManager.delegate = self
    locationsManager.desiredAccuracy = kCLLocationAccuracyBest // sets to best location accuracy
    locationsManager.requestWhenInUseAuthorization()// requests user location when app is opened
    locationsManager.startUpdatingLocation()// updates user location


  }

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation: CLLocation = locations[0]

    let latitude: CLLocationDegrees = userLocation.coordinate.latitude
    let longitude: CLLocationDegrees = userLocation.coordinate.longitude

    let latDelta: CLLocationDegrees = 0.05
    let longDelta: CLLocationDegrees = 0.05

    let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude)
    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    map.setRegion(region, animated: true)

    let pin = MKPointAnnotation()
    pin.coordinate.latitude = userLocation.coordinate.latitude
    pin.coordinate.longitude = userLocation.coordinate.longitude
    pin.title = "My current location"
    map.addAnnotation(pin) 
  }

算了,算了吧。在viewDidload()中:


map.showsUserLocation=true

您需要将属性
showsUserLocation
设置为true

MKMapView
可以为您显示用户位置和跟踪用户位置:

map.showsUserLocation = true
map.userTrackingMode = .follow

无需实施您自己的位置跟踪代理方法。

这是一种快速而简单的方法,但是如果我想向位置添加pin,该怎么办?在我使用locationManager功能之前,这取决于您希望如何添加pin。如果您只是想要一个pin而不是蓝色圆圈,那么您可以实现
MKMapViewDelegate
方法
mapView(\uuu:viewFor:)
并为用户位置注释返回pin视图。如果用户移动时需要一系列管脚,则可以使用最初的方法。