错误的用户位置

时间:2016-01-08 15:49:40

标签: ios swift

您好我刚开始学习swift ios-9。 我正在使用以下代码来获取用户的当前位置,但它正在获取位置

<+37.78583400,-122.40641700> +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/8/16, 3:38:10 PM Greenwich Mean Time

输出中没有在地图上显示我当前的位置。

Swift Code:

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!
    var manager:CLLocationManager!

    override func viewDidLoad() 
    {
        super.viewDidLoad()
        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
    }

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

        //userLocation - there is no need for casting, because we are now using CLLocation object

        let userLocation:CLLocation = locations[0]
        let latitude:CLLocationDegrees = userLocation.coordinate.latitude
        let longitude:CLLocationDegrees = userLocation.coordinate.longitude
        let latDelta:CLLocationDegrees = 0.01
        let lonDelta:CLLocationDegrees = 0.01
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)
    }
}

我还将CoreLocation.framawork链接到我的项目。 正如我刚开始学习的那样,我没有Apple的开发者许可证。

1 个答案:

答案 0 :(得分:1)

您无法模拟模拟器中的当前位置。您只能传递坐标,模拟器将模拟地图中的那些坐标。

要模拟自定义位置,请选择模拟器,然后从菜单中选择调试选项,并选择位置,然后选择自定义位置。

enter image description here

并输入您的自定义位置进行模拟:

enter image description here

要启用shouserLocation stting,请转到storyboard,然后选择地图视图,并在属性检查器中选择shwo用户位置选项。

enter image description here

注意:您当前的位置只能在真实设备上展示。