很抱歉没有提供代码。之所以在图片上发布图片,是因为xcode是 LLVM(低级虚拟机)编译器,主要具有 UI环境,尤其是对于配置部分。例如,通过将对象拖动到视图控制器来创建对象的出口或动作,而不是直接通过代码定义对象。因此,既然如此,我认为人们会轻易地更快地注意到我的错误。
import Cocoa
import MapKit
class ViewController: NSViewController, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var locManager = CLLocationManager()
var currentLocation = CLLocation()
var locationManager = CLLocationManager()
var didFindMyLocation = false
var strForCurLatitude = "";
var strForCurLongitude = "";
override func viewDidLoad() {
super.viewDidLoad()
let distancespan:CLLocationDegrees = 2000
let busCScampuslocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude)
mapView.setRegion(MKCoordinateRegion.init(center: bsuCScampuslocation, latitudinalMeters: distancespan, longitudinalMeters: distancespan), animated: true)
print(currentLocation.coordinate.latitude)
}
...
}
答案 0 :(得分:1)
我展示了一个ios演示,希望能为您提供帮助。 首先使用委托并定义CLLocationManager
CLLocationManagerDelegate
var locManager:CLLocationManager?;
然后这样做:
self.locManager = CLLocationManager();//[[CLLocationManager alloc] init];
self.locManager!.delegate = self;
self.locManager!.desiredAccuracy = kCLLocationAccuracyBest;
self.locManager!.distanceFilter = 5.0;
//ios8 later have effect
self.locManager?.requestWhenInUseAuthorization();// front use
self.locManager?.startUpdatingLocation();
最后,可以获得数据:
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
println("latitude %g",newLocation.coordinate.latitude);
println("longitude %g",newLocation.coordinate.longitude);
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("locationManager error");
}
最后但并非最不重要的一点,在info.plist中应添加以下内容:
NSLocationWhenInUseUsageDescription,set YES
希望能为您提供帮助。
答案 1 :(得分:0)
迅速5:
ViewController.swift
import Cocoa
import CoreLocation
import MapKit
class ViewController: NSViewController, CLLocationManagerDelegate {
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
//This is where you can update the MapView when the computer is moved (locations.last!.coordinate)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
print("location manager auth status changed to: " )
switch status {
case .restricted:
print("restricted")
case .denied:
print("denied")
case .authorized:
print("authorized")
case .notDetermined:
print("not yet determined")
default:
print("Unknown")
}
}
将这些行添加到Info.plist或将NSLocationAlwaysAndWhenInUseUsageDescription
和/(或?)NSLocationUsageDescription
设置为纯文本形式,以说明需要访问用户位置的原因
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Allows us to locate you</string>
<key>NSLocationUsageDescription</key>
<string>Allows us to locate you</string>
如果用户关闭了位置服务,则授权状态将为“拒绝”