WatchKit 和地理定位
WatchKit and Geolocation
我正在 Swift 上为 Apple Watch 编写应用程序。我的目标是创建一种导航器,它可以显示您当前位置与地图上某个点之间的距离。目前我正在尝试获取我当前的位置并将其显示在屏幕上但不幸的是代码不起作用。
这里是 InterfaceController.swift 文件的代码。
class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {
@IBOutlet weak var lblLocationDisplay: WKInterfaceLabel!;
let locationManager = CLLocationManager();
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func btnFindLocation() {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.requestWhenInUseAuthorization();
locationManager.startUpdatingLocation();
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
println("Something");
let placeMarks = placemarks[0] as CLPlacemark;
self.displayLocation(placeMarks);
});
}
func displayLocation(placemark: CLPlacemark) {
// locationManager.stopUpdatingLocation();
println(placemark.country);
let locality = placemark.locality;
let postalCode = placemark.postalCode;
let administrativeArea = placemark.administrativeArea;
let country = placemark.country;
lblLocationDisplay.setText("Locality: \(locality) PostalCode: \(postalCode) Area: \(administrativeArea) Country: \(country)");
}
}
这里是project
请分享您对代码不起作用的原因的建议。我也希望你能建议我如何在手表中实现导航应用程序。谢谢!
使用时需要使用request always access而不是request。有关在 WatchKit 扩展中使用 CLLocationManger 的更多信息,请参阅此 post How to calculate current location in WatchKit extension。
要获取当前用户位置,您应该按照以下步骤操作:
- 将 CoreLocation 框架添加到 IOS 应用程序的库中。
- 在 IOS 应用程序的支持文件夹和 info.plist 文件中,您应该添加两行名称:
NSLocationWhenInUseDescription 和您可以设置的值 "To know your locaton!!"
和另一行:
NSLocationAlwaysUsageDescription 和您可以设置的值 "Always using location!"
- 现在导入 CoreLocation 以查看 controller.swift 并添加 CLLocationDelegate 的委托。
现在在 watchkit 扩展中,您也应该导入 CoreLocation 和 CLLocationManager Delegate 并在 awakeWithContext 中执行此步骤:
//Make a new locationManager variable:
var locationManager = CLLocationManager()
// set delegate
locationManager.delegate = self
// Set desiredAccuracy using a GPS of IP:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// request to use location
locationManager.requestAlwaysAuthorization()
// start update location
locationManager.startUpdatingLocation()
之后编写 CLLocationManagerDelegate:
// MARK: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println(locations)
}
请注意,您 运行 您的 IOS 应用程序首先接受获取用户的位置,之后您可以 运行 Watchkit 方案获取当前用户位置。
我正在 Swift 上为 Apple Watch 编写应用程序。我的目标是创建一种导航器,它可以显示您当前位置与地图上某个点之间的距离。目前我正在尝试获取我当前的位置并将其显示在屏幕上但不幸的是代码不起作用。
这里是 InterfaceController.swift 文件的代码。
class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {
@IBOutlet weak var lblLocationDisplay: WKInterfaceLabel!;
let locationManager = CLLocationManager();
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func btnFindLocation() {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.requestWhenInUseAuthorization();
locationManager.startUpdatingLocation();
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in
println("Something");
let placeMarks = placemarks[0] as CLPlacemark;
self.displayLocation(placeMarks);
});
}
func displayLocation(placemark: CLPlacemark) {
// locationManager.stopUpdatingLocation();
println(placemark.country);
let locality = placemark.locality;
let postalCode = placemark.postalCode;
let administrativeArea = placemark.administrativeArea;
let country = placemark.country;
lblLocationDisplay.setText("Locality: \(locality) PostalCode: \(postalCode) Area: \(administrativeArea) Country: \(country)");
}
}
这里是project
请分享您对代码不起作用的原因的建议。我也希望你能建议我如何在手表中实现导航应用程序。谢谢!
使用时需要使用request always access而不是request。有关在 WatchKit 扩展中使用 CLLocationManger 的更多信息,请参阅此 post How to calculate current location in WatchKit extension。
要获取当前用户位置,您应该按照以下步骤操作: - 将 CoreLocation 框架添加到 IOS 应用程序的库中。 - 在 IOS 应用程序的支持文件夹和 info.plist 文件中,您应该添加两行名称: NSLocationWhenInUseDescription 和您可以设置的值 "To know your locaton!!" 和另一行: NSLocationAlwaysUsageDescription 和您可以设置的值 "Always using location!" - 现在导入 CoreLocation 以查看 controller.swift 并添加 CLLocationDelegate 的委托。
现在在 watchkit 扩展中,您也应该导入 CoreLocation 和 CLLocationManager Delegate 并在 awakeWithContext 中执行此步骤:
//Make a new locationManager variable:
var locationManager = CLLocationManager()
// set delegate
locationManager.delegate = self
// Set desiredAccuracy using a GPS of IP:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// request to use location
locationManager.requestAlwaysAuthorization()
// start update location
locationManager.startUpdatingLocation()
之后编写 CLLocationManagerDelegate:
// MARK: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println(locations)
}
请注意,您 运行 您的 IOS 应用程序首先接受获取用户的位置,之后您可以 运行 Watchkit 方案获取当前用户位置。