使用xcode swift 3从osx/cocoa应用程序获取当前位置

使用xcode swift 3从osx/cocoa应用程序获取当前位置,xcode,cocoa,swift3,mapkit,Xcode,Cocoa,Swift3,Mapkit,我为没有提供代码而道歉。我之所以将其与图片一起发布,是因为xcode是LLVM(低级虚拟机)编译器,它主要拥有UI环境,尤其是配置部分。例如,通过将对象拖动到视图控制器(而不是直接通过代码定义)来创建对象的出口或操作。因此,既然如此,我想人们会很容易更快地注意到我的错误 import Cocoa import MapKit class ViewController: NSViewController, CLLocationManagerDelegate { @IBOutlet var

我为没有提供代码而道歉。我之所以将其与图片一起发布,是因为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)
   }
   ...
}
Swift 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或将
nsLocationAlways和WhenInUsageDescription
和/(或?
NSLocationUsageDescription
设置为需要访问用户位置的明文说明

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
        <string>Allows us to locate you</string>
    <key>NSLocationUsageDescription</key>
        <string>Allows us to locate you</string>
n位置始终和何时使用说明
允许我们找到你
NSLocationUsageDescription
允许我们找到你

如果用户关闭了定位服务,授权状态将为“拒绝”

请不要发布图像、邮编(文本)。您的
currentLocation
被声明为空。您必须设置
CLLocationManager
的委托,然后调用
startUpdatingLocation()
通过
didUpdateLocations
委托方法获取当前位置。请阅读我感谢您的回复的文档,但您是否可以用代码解释如何设置CLLocationManager的委托?或者你可以在回答部分回复。你在macos上试过吗?