为 osx 设置地图以及用户当前位置
Setting map along with user current location for osx
问候专家,
据我所知,我们可以使用 google map api for IOS 制作地图,就像下面的代码一样。
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 应用程序无法获取当前位置吗?如果不是,那么如何在 cocoa 应用程序中获取当前位置?
我很确定许多像我这样的 xcode 程序员都试图解决这个问题。任何答案你都会得到很大的赞赏。 :)
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)" )
}
}
如果您在第一次启动应用程序时对 "enable location services" 提示说是,则在 macOS 上对我有用。
请注意,您还需要 NSLocationWhenInUseUsageDescription
属性 列表条目,如文档中所示。
控制台输出是(稍微混淆):
location manager auth status changed to: status not yet determined
location manager auth status changed to: status authorized location:
Optional(<+4X.48,-12X.62632228> +/- 65.00m (speed -1.00 mps /
course -1.00) @ 11/3/18, 11:42:48 AM Pacific Daylight Time)
问候专家,
据我所知,我们可以使用 google map api for IOS 制作地图,就像下面的代码一样。
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 应用程序无法获取当前位置吗?如果不是,那么如何在 cocoa 应用程序中获取当前位置?
我很确定许多像我这样的 xcode 程序员都试图解决这个问题。任何答案你都会得到很大的赞赏。 :)
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)" )
}
}
如果您在第一次启动应用程序时对 "enable location services" 提示说是,则在 macOS 上对我有用。
请注意,您还需要 NSLocationWhenInUseUsageDescription
属性 列表条目,如文档中所示。
控制台输出是(稍微混淆):
location manager auth status changed to: status not yet determined location manager auth status changed to: status authorized location: Optional(<+4X.48,-12X.62632228> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/3/18, 11:42:48 AM Pacific Daylight Time)