问候专家,
据我所知,我们可以使用IOS的google map api来制作地图,如下所示。
import UIKit
import GoogleMaps
/* For cocoa:
Import Cocoa
Import MapKit
*/
class ViewController: NSViewController, CLLocationManagerDelegate {
@IBOutlet var mapView: MKMapView!
var locationManager = CLLocationManager()
var didFindMyLocation = false
var strForCurLatitude = "";
var strForCurLongitude = "";
var currentLocation = locManager.location!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
print("User allowed us to access location")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error while get location \(error)")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation? = locationManager.location
let coordinate: CLLocationCoordinate2D? = location?.coordinate
print(coordinate!)
print(coordinate!.latitude)
print(coordinate!.longitude)
strForCurLatitude = "\(coordinate!.latitude)"
strForCurLongitude = "\(coordinate!.longitude)"
let camera = GMSCameraPosition.camera(withLatitude: coordinate!.latitude, longitude: coordinate!.longitude, zoom: 15)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.isMyLocationEnabled = true
self.view = mapView
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(coordinate!.latitude, coordinate!.longitude)
marker.map = mapView
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
但是当我尝试使用类似的osx方法时(区别是我使用的是mapkit而不是google map ),它说 requestWhenInUseAuthorization()'不可用。我使用了该线程How to get user location ? [macOS],但似乎无法明确解决它是否可用于获取osx的当前位置的问题。那么,无法获取macOS / cocoa应用程序的当前位置吗?如果不是,那么如何在可可应用中获取当前位置?
我敢肯定,像我这样的许多xcode程序员都试图解决这个问题。任何答案您都会得到极大的赞赏。 :)
答案 0 :(得分:0)
requestWhenInUseAuthorization
在macOS上不可用。
这是NSViewController子类的源,该子类在macOS 10.13.6的Xcode 10.1中成功检查了位置管理器和当前位置:
import Cocoa
import MapKit
import CoreLocation
class MapViewController: NSViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
print("location manager auth status changed to:" )
switch status {
case .restricted:
print("status restricted")
case .denied:
print("status denied")
case .authorized:
print("status authorized")
let location = locationManager.location
print("location: \(String(describing: location))")
case .authorizedAlways:
print("status authorized always")
case .notDetermined:
print("status not yet determined")
}
}
func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
print( "location manager failed with error \(error)" )
}
}
如果您在首次启动应用程序时对“启用位置服务”提示说“是”,则在macOS上为我工作。
请注意,您还需要NSLocationWhenInUseUsageDescription
属性列表条目,如文档中所述。
控制台输出(被稍微混淆):
位置管理器身份验证状态更改为:状态尚未确定 位置管理员身份验证状态更改为:状态授权位置: 可选(<+ 4X.48,-12X.62632228> +/- 65.00m(速度-1.00 mps / 课程-1.00)@ 181/3 / 18,11:42:48 AM太平洋夏令时间)