Calculate ETA in Swift

旧时模样 提交于 2019-12-12 14:15:11

问题


I'm attempting to calculate the estimated travel time (walking) between two locations in swift, the user to an annotation.

Here is my current code, it does not throw any errors that crash the program but only returns "Error while requesting 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)

}
}

The console looks something like this for an exceedingly long amount of lines

    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

I'm hoping for some guidance, thank you all!

Note: ViewController calls this class to be made many times in a row so not sure if that would cause Apple to reject ETA requests?

来源:https://stackoverflow.com/questions/44208558/calculate-eta-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!