【发布时间】:2017-12-15 16:04:43
【问题描述】:
我是 IOS 新手。我正在尝试使用地图来获取用户的当前位置。我正在关注的教程适用于 IOS 10。
我浏览了这篇文章并做了它所说的一切,但它仍然不起作用 Location Services not working in iOS 11
以下是我的代码
class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
var locationManager=CLLocationManager()
// @IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate=self
locationManager.desiredAccuracy=kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let latitude=userLocation.coordinate.latitude
let longitude=userLocation.coordinate.longitude
let latDelta:CLLocationDegrees=0.05
let lonDelta:CLLocationDegrees=0.05
let span=MKCoordinateSpan(latitudeDelta:latDelta, longitudeDelta:lonDelta)
let location=CLLocationCoordinate2D(latitude:latitude,longitude:longitude)
let region=MKCoordinateRegion(center:location,span:span)
self.mapView.setRegion(region,animated:true)
}
}
我在 info.plist 文件中添加了以下内容
<key>NSLocationAlwaysUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
任何帮助将不胜感激。
【问题讨论】:
-
定义“不起作用”。这不是一个有用的描述。您发布的代码的确切问题是什么?
-
我无法获取用户的当前位置。它甚至没有给我权限对话框?
-
你是在模拟器还是真机上工作?如果模拟器(并提供 GPS 在那里工作),请尝试重置它,然后重新安装它。如果是设备,请尝试删除该应用并重新安装。
-
@dfd GPS 确实可以在模拟器中工作,但是在菜单栏中,您需要转到 Debug -> Location -> 然后进行自定义或预定义。
-
一个 ios 项目有几个 info.plist 文件,确保你编辑的是正确的!