2017-06-13 78 views
0

我是新来的应用程序开发,我试图让用户位置来模拟器,但我不断得到使用未解决的标识符错误。我看过其他问题,这些问题对于他们自己的项目非常具体,并试图以类似的方式来处理我自己的应用程序,但无济于事。任何帮助,por支持?这里是我的代码,并链接到Xcode的屏幕截图。用户位置不会出现在模拟器在Xcode

import UIKit 
import MapKit 
import CoreLocation 

class SecondViewController: UIViewController, CLLocationManagerDelegate { 

    //Map 
    @IBOutlet weak var map: MKMapView! 

    let manager = CLLocationManager() 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { let location = locations[0] 

     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01) 

     let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 

     let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 


     self.map.showsUserLocation = true 

    } 

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

     //This is where I get an error 
     map.setRegion(region, animated: true)** 
    } 

Error: Use of unresolved identifier

screenshot

+0

你需要在xcode中的userLocation –

+1

你有没有在模拟器中设置自定义位置? 如果没有调试 - >位置 - >自定义位置 设置并尝试此设置 设置位置之前的设备。所以你需要在经过设备采取的纬度经过之后设置 –

回答

3

let region:MKCoordinateRegion本地标识符您的委托方法中:

func locationManager(_:didUpdateLocations) {...} 

这就是为什么你会得到使用未解决的标识符。在整个课程中使此identifier可访问,错误将消失。像:

class SecondViewController: UIViewController, CLLocationManagerDelegate { 

    //Map 
    @IBOutlet weak var map: MKMapView! 
    var region = MKCoordinateRegion() 
    ....... 
    ....... 
} 

N.B:但是,这不会让你看到在地图上任何东西。看到来自MapView任何输出最好的办法是把你的map.setRegion(region, animated: true)方法delegate里面:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let location = locations.first 
    let span = MKCoordinateSpanMake(0.01, 0.01) 
    let myLocation = CLLocationCoordinate2DMake((location?.coordinate.latitude)!, (location?.coordinate.longitude)!) 
    region = MKCoordinateRegionMake(myLocation, span) 
    map.setRegion(region, animated: true) 
} 
0

使用MKCoordinateRegion实例之前viewdidload方法 您可以按照下面的代码以供参考。

class Test: UIViewController { 
    var region = MKCoordinateRegion() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     region = MKCoordinateRegionMake(myLocation, span) 
    } 
} 
0

你得到错误因为

  1. 内部viewDidLoad中()方法do not have access区域内 viewDidLoad中()方法可变可变区

    //This is where I get an error 
        map.setRegion(region, animated: true)** 
    

解决方案

  1. 区域变量全局类,以便可以从任何地方访问你想。
  2. 而是管道的内部FUNC的LocationManager

    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation,span) 
    

使用

self.region = MKCoordinateRegionMake(myLocation,span) 

这里是完整的代码,你可以使用。

import UIKit 
import MapKit 
import CoreLocation 

class SecondViewController: UIViewController, CLLocationManagerDelegate { 

    //Map 
    @IBOutlet weak var map: MKMapView! 
    //global variable 
    var region:MKCoordinateRegion = MKCoordinateRegion() 

    let manager = CLLocationManager() 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { let location = locations[0] 

     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01) 

     let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) 

     self.region = MKCoordinateRegionMake(myLocation, span) 


     self.map.showsUserLocation = true 

    } 

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

     //This is where I get an error 
     map.setRegion(region, animated: true)** 
    } 
// 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

如果你正在使用模拟器更重要的,那么你必须启用位置的仿真。

+0

这个答案与我的答案有什么不同? – nayem

相关问题