位置管理器在模拟器中不工作但是 运行 在 Iphone 中正常工作
Location Manager not work in simulator But run in Iphone properly
App 运行s in iPhone 但是当我试图在模拟器中 运行 它显示错误如图
请帮助我并提前谢谢你
// 这是我的代码:
locationManager.delegate = self
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
{
let latitude1 = locManager.location!.coordinate.latitude.description
latitude = latitude1
let longitude1 = locManager.location!.coordinate.longitude.description
longnitude = longitude1
print(latitude)
print(longnitude)
} else {
latitude = ""
longnitude = ""
}
您必须将框架导入为
import CoreLocation
试试这个代码:在 Xcode 模拟器中测试(Swift 3)
更新您的 plist:
隐私 - 使用时的位置使用说明 == 一些值
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
print(location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Errors: " + error.localizedDescription)
}
}
代码输出:
App 运行s in iPhone 但是当我试图在模拟器中 运行 它显示错误如图
请帮助我并提前谢谢你
// 这是我的代码:
locationManager.delegate = self
var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
{
let latitude1 = locManager.location!.coordinate.latitude.description
latitude = latitude1
let longitude1 = locManager.location!.coordinate.longitude.description
longnitude = longitude1
print(latitude)
print(longnitude)
} else {
latitude = ""
longnitude = ""
}
您必须将框架导入为
import CoreLocation
试试这个代码:在 Xcode 模拟器中测试(Swift 3)
更新您的 plist:
隐私 - 使用时的位置使用说明 == 一些值
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
print(location)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Errors: " + error.localizedDescription)
}
}
代码输出: