Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/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
当前位置未显示在iOS模拟器中_Ios_Swift_Location_Mkmapview - Fatal编程技术网

当前位置未显示在iOS模拟器中

当前位置未显示在iOS模拟器中,ios,swift,location,mkmapview,Ios,Swift,Location,Mkmapview,我试图在MKMapView中显示用户的当前位置,mapView。我请求许可,我相信我所有的基地都被覆盖了,但由于某种原因,地图上没有显示当前位置 下面是代码。不要介意线段控件,我只是使用它们来更改贴图类型 import UIKit import MapKit let annotationId = "annotationID"; class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak

我试图在MKMapView中显示用户的当前位置,
mapView
。我请求许可,我相信我所有的基地都被覆盖了,但由于某种原因,地图上没有显示当前位置

下面是代码。不要介意线段控件,我只是使用它们来更改贴图类型

import UIKit
import MapKit

let annotationId = "annotationID";

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segControls: UISegmentedControl!

let locationManager = CLLocationManager();
var zoomToUserLocation = false;

// Switch statements for the seg controls.

@IBAction func mapType(sender: UISegmentedControl) {
    switch segControls.selectedSegmentIndex {
    case 0:
        mapView.mapType = MKMapType.Standard
    case 1:
        mapView.mapType = MKMapType.Satellite
    case 2:
        mapView.mapType = MKMapType.Hybrid
    default:
        mapView.mapType = MKMapType.Standard 
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // We are setting the delegate equal to self here to be able to use the indentifier that deques the annotations.
    locationManager.delegate = self

    // Status of user is variable that speaks directly to (in this case) authorization status
    let status = CLLocationManager.authorizationStatus()

    if status == CLAuthorizationStatus.NotDetermined {
        locationManager.requestWhenInUseAuthorization()
    } else if  status != .Denied {
        locationManager.startUpdatingLocation()
    }

    // let annotation = MKPointAnnotation();
}

// If the status changes to authorize when in use or always authorize
// then start updating the location, if not don't do anything.

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.AuthorizedAlways {
        locationManager.startUpdatingLocation()
    }
}

// If the location failed when trying to get users location execute this function
func  locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("Error: \(error)")
    }
}

extension ViewController: MKMapViewDelegate {
    func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {

    let coordinate = userLocation.coordinate; // this sets the var coordinates to the location of the user.

    println("User Location = (\(coordinate.latitude), \(coordinate.longitude))");

    if zoomToUserLocation == true {
        let locationOfDevice: CLLocation = userLocation.location // this determines the location of the device using the users location

        let deviceCoordinate: CLLocationCoordinate2D = locationOfDevice.coordinate // determines the coordinates of the device using the location device variabel which has in it the user location.

        let span = MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1) // this determines the span in which its determined that 1 degree is 69 miles.

        let region = MKCoordinateRegion(center: deviceCoordinate, span: span) // set the center equal to the device coordinates and the span equal to the span variable we have created this will give you the region.

        mapView.setRegion(region, animated: true);
    } 
}
}

有时,您需要先设置一个自定义位置,以使模拟器更新并实际设置一个位置(不知道为什么)。我要做的是进入
Debug->Location->Apple
,检查地图位置的功能是否有效。然后,我使用从google maps或类似工具中找到的坐标设置了一个自定义位置。

您需要将MKMapView的
showsUserLocation
属性设置为
true
。它是一个布尔属性,将显示当前位置(您所指的脉冲蓝点,可以着色,但不总是蓝色)

以下是苹果公司在其报告中的总结

此属性不指示用户的位置在地图上是否实际可见,仅指示地图视图是否应尝试显示它。将此属性设置为
true
会导致地图视图使用核心位置框架查找当前位置,并尝试在地图上显示它。只要此属性为
true
,地图视图将继续跟踪用户的位置并定期更新。此属性的默认值为
false

显示用户的位置并不保证该位置在地图上可见。用户可能已将地图滚动到其他点,导致当前位置在屏幕外。要确定用户的当前位置是否显示在地图上,请使用
userLocationVisible
属性

因此,在
viewDidLoad
函数中,您应该设置以下属性:

mapView.showsUserLocation = true

zoomToUserLocation
属性无法控制当前位置点。

谢谢。在多次尝试使用“Apple”位置失败后,我才将位置更改为custom。尽管如此,它还是不起作用。好吧,看看@Sam的回答,这应该对你有帮助:)