【发布时间】:2017-12-08 14:07:54
【问题描述】:
在我的 viewDidLoad 中,我检索当前位置和所选商店的位置(该位置通过 Geofire 存储在 Firebase 数据库中) 我已经在 viewDidLoad 中显示了计算的 ETA 和距离,但是当我走出去并沿着路线行驶时,它并没有更新。每当用户移动时,如何更新 DirectionsLbl 和 etaLbl?有什么建议么?我已经发布了我的 viewDidLoad 和 didUpdateLocations 函数。提前致谢。
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let numberFormatter = NumberFormatter()
numberFormatter.minimumFractionDigits = 2
numberFormatter.maximumFractionDigits = 2
numberFormatter.minimumIntegerDigits = 1
let userLat = locationManager.location?.coordinate.latitude
let userLon = locationManager.location?.coordinate.longitude
myLocation = CLLocation.init(latitude: userLat!, longitude: userLon!)
manager.stopUpdatingLocation()
// Create request
self.request.destination = self.destinationMapItem
self.request.source = self.self.sourceMapItem!
self.request.transportType = MKDirectionsTransportType.walking
self.request.requestsAlternateRoutes = false
let directions = MKDirections(request: self.request)
directions.calculate { response, error in
if let route = response?.routes.first {
let time = self.secondsToHoursMinutesSeconds(seconds: route.expectedTravelTime)
print("Distance: \(route.distance), ETA: \(time)")
self.etaLbl.text = "\(time)mins"
let miles = route.distance / 1609.344
self.distanceLbl.text = "\(String(describing: numberFormatter.string(from: miles as NSNumber)!))m"
for step in route.steps {
self.directionslbl.text = step.instructions
}
self.mapView.camera.altitude = 500
self.mapView.add(route.polyline)
} else {
print(error?.localizedDescription)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.showsBuildings = false
self.mapView.isRotateEnabled = false
self.mapView.showsCompass = false
self.mapView.showsPointsOfInterest = false
let numberFormatter = NumberFormatter()
numberFormatter.minimumFractionDigits = 2
numberFormatter.maximumFractionDigits = 2
numberFormatter.minimumIntegerDigits = 1
geoFire = GeoFire(firebaseRef: ref.child("Store Locations"))
print(store!)
self.orderIDLbl.text = orderID!
self.storeLbl.text = store!
geoFire?.getLocationForKey(store!, withCallback: { (location, error) in
if error != nil {
print("ERROR IS: \(String(describing: error?.localizedDescription))")
} else {
self.destinationStore = location
print("LOCATION OF STORE IS: \(String(describing: self.destinationStore!))")
// Get destination position
let storeLong = self.destinationStore?.coordinate.longitude
let storeLat = self.destinationStore?.coordinate.latitude
let storeLocation = CLLocation.init(latitude: storeLat!, longitude: storeLong!)
let destinationPlacemark = MKPlacemark(coordinate: storeLocation.coordinate, addressDictionary: nil)
let anno = StoreAnnotation(coordinate: storeLocation.coordinate, storeName: self.store!)
self.mapView.addAnnotation(anno)
self.destinationMapItem = MKMapItem(placemark: destinationPlacemark)
self.request.destination = self.destinationMapItem
self.request.source = self.self.sourceMapItem!
self.request.transportType = MKDirectionsTransportType.walking
self.request.requestsAlternateRoutes = false
let directions = MKDirections(request: self.request)
directions.calculate { response, error in
if let route = response?.routes.first {
let time = self.secondsToHoursMinutesSeconds(seconds: route.expectedTravelTime)
print("Distance: \(route.distance), ETA: \(time)")
self.etaLbl.text = "\(time)mins"
let miles = route.distance / 1609.344
self.distanceLbl.text = "\(String(describing: numberFormatter.string(from: miles as NSNumber)!))m"
self.directionsArray = route.steps
print(route.steps)
for step in route.steps {
self.directionslbl.text = step.instructions
}
//self.mapView.camera.heading = self.directionsArray[0].
self.mapView.add(route.polyline)
} else {
print(error?.localizedDescription)
}
}
}
})
let userLat = locationManager.location?.coordinate.latitude
let userLon = locationManager.location?.coordinate.longitude
myLocation = CLLocation.init(latitude: userLat!, longitude: userLon!)
mapView.delegate = self
mapView.userTrackingMode = .followWithHeading
mapView.camera.altitude = 500
mapView.camera.pitch = 30
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
// Get current position
let sourcePlacemark = MKPlacemark(coordinate: (myLocation?.coordinate)!, addressDictionary: nil)
self.sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let orderStatusRef = ref.child("Users").child(currentUserID!).child("Current Order").child(orderID!)
orderStatusRef.observe(.value) { (DataSnapshot) in
let dict = DataSnapshot.value as? NSDictionary
let orderStatus = dict!["Order Status"] as? Int
switch orderStatus! {
case 0:
self.orderStatusLbl.text = "Received"
self.orderStatusLbl.textColor = UIColor.red
case 1:
self.orderStatusLbl.text = "Being Prepared"
self.orderStatusLbl.textColor = UIColor.orange
default:
break
}
}
}
【问题讨论】:
-
我没有运行您的代码,但是您在 DidUpdateLocations 中调用了 StopUpdatingLocations,所以我认为您不会获得多个位置更新?
-
另外,您对当前位置的设置应该来自“locations”参数(set loc = locations.last!)
标签: ios swift xcode geolocation mapkit