gpt4 book ai didi

xcode - 设置 osx 的 map 以及用户当前位置

转载 作者:行者123 更新时间:2023-12-03 17:09:06 25 4
gpt4 key购买 nike

问候专家,

据我所知,我们可以使用适用于 IOS 的谷歌地图 api 制作 map ,如下面的代码所示。

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)" )
}
}

如果您在第一次启动应用程序时对“启用位置服务”提示选择"is",则适用于 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)

关于xcode - 设置 osx 的 map 以及用户当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52828640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com