我试图快速计算两个位置之间的估计旅行时间(步行),用户进行注释。
这是我当前的代码,它不会引发任何导致程序崩溃的错误,而只会返回“请求 ETA 时出错”
import UIKit
import MapKit
class LocationObjects: NSObject, MKAnnotation {
/*let userView = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
let annotationPoint = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
let distance = userView.distance(from: annotationPoint)*/
var identifier = "bathroom location"
var title: String?
var coordinate: CLLocationCoordinate2D
var distance: Double
var phoneNumber: String
var addressFinished: String
var estimatedTravelTime: String
// Request ETA function
// Uses user location and destination to send request
// For Estimated time of arrival
class func requestETA(userCLLocation: CLLocation, coordinate: CLLocationCoordinate2D) -> String {
var travelTime = String()
let request = MKDirectionsRequest()
/* Source MKMapItem */
let sourceItem = MKMapItem(placemark: MKPlacemark(coordinate: userCLLocation.coordinate, addressDictionary: nil))
request.source = sourceItem
/* Destination MKMapItem */
let destinationItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary: nil))
request.destination = destinationItem
request.requestsAlternateRoutes = false
// Looking for walking directions
request.transportType = MKDirectionsTransportType.walking
// You use the MKDirectionsRequest object constructed above to initialise an MKDirections object
let directions = MKDirections(request: request)
directions.calculateETA { (etaResponse, error) -> Void in
if let error = error {
print("Error while requesting ETA : \(error.localizedDescription)")
travelTime = "Not Available"
}else{
print("No error requesting ETA")
travelTime = "\(Int((etaResponse?.expectedTravelTime)!/60)) min"
}
}
return travelTime
}
init(name:String,lat:CLLocationDegrees,long:CLLocationDegrees,userCLLocation:CLLocation, phone:String, address:String){
title = name
coordinate = CLLocationCoordinate2DMake(lat, long)
distance = Double(String(format: "%.2f", (userCLLocation.distance(from: CLLocation(latitude: lat, longitude: long)))*0.000621371))!
phoneNumber = phone
addressFinished = address
estimatedTravelTime = LocationObjects.requestETA(userCLLocation: userCLLocation, coordinate: coordinate)
}
}
对于非常长的行,控制台看起来像这样
2017-05-26 13:53:51.583 Toilet Locator[2568:181698] didFailWithError Error Domain=GEOErrorDomain Code=-3 "(null)" UserInfo={GEORequestThrottleStateLevel=0, GEORequestThrottleStateResetTimeRemaining=34.019131004810333}
2017-05-26 13:53:51.584 Toilet Locator[2568:181698] didFailWithError Error Domain=GEOErrorDomain Code=-3 "(null)" UserInfo={GEORequestThrottleStateLevel=0, GEORequestThrottleStateResetTimeRemaining=34.018206000328064}
Error while requesting ETA : Directions Not Available
Error while requesting ETA : Directions Not Available
希望大家给予指导,谢谢大家!
注意:ViewController 连续多次调用此类,因此不确定这是否会导致 Apple 拒绝 ETA 请求?