我是IOS的新手。我正在尝试使用地图获取用户的当前位置。我正在关注的教程适用于IOS 10。
我仔细阅读了这篇文章并做了所有说明,但仍然无法正常工作 位置服务在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)
}
}
Run Code Online (Sandbox Code Playgroud)
我在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>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
Dal*_*las 11
如果使用模拟器,请确保已选择位置。选择模拟器,在菜单栏中,您需要转到Debug-> Location->然后执行自定义或预定义。
如果您这样做了,请尝试以下方法。
与其在info.plist中手动输入该文本,不如使用默认编辑器将其打开。
单击蓝色突出显示区域中显示的加号按钮(您可以选择其中任何一个),这将添加一个新行。

| 归档时间: |
|
| 查看次数: |
9541 次 |
| 最近记录: |