我是应用程序开发的新手,我试图让用户位置出现在模拟器中,但我一直得到使用未解析的标识符错误。我已经查看了其他问题,这些问题对于他们自己的项目非常具体,并试图以类似的方式处理我自己的应用程序,但无济于事。任何帮助,赞成?这是我的代码,以及指向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
答案 0 :(得分: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)
}
答案 1 :(得分:0)
在MKCoordinateRegion
方法之前使用viewdidload
实例
您可以按照以下代码进行参考。
class Test: UIViewController {
var region = MKCoordinateRegion()
override func viewDidLoad() {
super.viewDidLoad()
region = MKCoordinateRegionMake(myLocation, span)
}
}
答案 2 :(得分:0)
您收到错误是因为
viewDidLoad() 区域变量的方法do not have access
内的变量区域
viewDidLoad()方法
//This is where I get an error
map.setRegion(region, animated: true)**
解决方案:
而不是 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.
}
}
更重要的是,如果您使用模拟器,则必须启用位置模拟。