我在更新用户位置时遇到问题。我正在构建应用程序,它显示最近的垃圾桶以及它们与用户之间的距离。我正在使用这个位置管理器,但它永远不会更新位置。即使用户移动,存储的位置也保持不变
Xamarin.iOS
我用它来计算距离
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
//store myLocation in UserDefaults
UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.latitude, forKey: "latitude")
UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.longitude, forKey: "longitude")
UserDefaults.standard.set(myLocation.latitude, forKey: "latitude")
UserDefaults.standard.set(myLocation.longitude, forKey: "longitude")
UserDefaults().synchronize()
self.mapView.showsUserLocation = true
}
在应用程序内部这不是一个很大的问题,但我需要一个不断更新的位置,以便小部件正常工作。这个小部件显示了三个最近的垃圾桶,并且如果使用旧的位置数据,它实际上并没有做它应该做的事情。
这是我完整的ViewController:
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.value(forKey: "latitude") as! CLLocationDegrees, UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.value(forKey: "longitude") as! CLLocationDegrees)
// geeft zin met afstand van locatie1 tot myLocation
func afstandPuntTotUser(locatie1: CLLocationCoordinate2D) -> String {
manager.startUpdatingLocation()
let cllocatie1 = converter(locatie: locatie1)
let myLocation = converter(locatie: myLocation)
let afstand = cllocatie1.distance(from: myLocation)
let afgerondeafstand = afstand - afstand.truncatingRemainder(dividingBy: 1.0) //zorgt voor 1 kommagetal
if afstand < 1000.0 {
return "De afstand tot de bak is \(afgerondeafstand) meter"
}
else {
return "De afstand tot de bak is \(afgerondeafstand / 1000 - (afgerondeafstand / 1000).truncatingRemainder(dividingBy: 0.1)) kilometer"
}
}
答案 0 :(得分:3)
在同一个Project中创建一个新类,并在新类中添加此代码。然后检查是否会调用didUpdateLocations方法。让我知道这段代码是否适合你。
@IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations[0]
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
//let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
//let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
//store myLocation in UserDefauls
UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.latitude, forKey: "latitude")
UserDefaults(suiteName: "group.Afvalbakfinder.Jordadema.userLocation")?.set(myLocation.longitude, forKey: "longitude")
UserDefaults.standard.set(myLocation.latitude, forKey: "latitude")
UserDefaults.standard.set(myLocation.longitude, forKey: "longitude")
UserDefaults().synchronize()
//region wel of niet? nog even over hebben
//mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Failed to find user's location: \(error.localizedDescription)")
}
答案 1 :(得分:0)
检查以下内容: