Lee*_*fin 8 mkmapview ios ios-simulator ios8 xcode7
我正在使用XCode v7.2.1,Simulator v9.2.
我有一个UIViewController,它显示一个地图并且应该获取我的位置并在地图上显示:
import UIKit
import MapKit
class LocationVC: UIViewController, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
}
override func viewDidAppear(animated: Bool) {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
map.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我NSLocationWhenInUseUsageDescription在info.plist中添加了如下所示:
我还选择了调试 - >位置 - >自定义位置...并设置芬兰赫尔辛基的经度和纬度,如下所示:

当我运行我的应用程序时,会显示地图,但它不会获取我的位置.为什么?(我的意思是我没有在地图的任何地方看到蓝点).
===== UPDATE ====
我的应用程序运行时也尝试了这个,但它也没有用.
您正在请求用户的位置,但实际上没有对响应做任何事情.成为位置经理的代表并回应授权变更.
这段代码适用于7.2.1(在Debug - > Location中选择"Apple"之后):
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
map.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
guard status == .AuthorizedWhenInUse else { print("not enabled"); return }
map.showsUserLocation = true
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2620 次 |
| 最近记录: |