I’m developing an iOS app using MapKit to calculate travel times between two coordinates.
Everything works perfectly when I test with coordinates in the US, Japan, or Hong Kong if my network is outside mainland China.
However, when my device (or simulator) is connected to a mainland China network and always fails with: “Directions Not Available“
Some test result:
Hong Kong → Hong Kong: ✅ works
Hong Kong → Shanghai: ✅ works
Shanghai → Hong Kong: ✅ works
Japan (Tokyo → Tokyo): ❌ fail
US (New York → New York): ❌ fail
struct TravelTimeCalculator {
static func calculateTravelTime(
from: CLLocationCoordinate2D,
to: CLLocationCoordinate2D
) async throws -> TimeInterval {
let sourcePlacemark = MKPlacemark(coordinate: from)
let destPlacemark = MKPlacemark(coordinate: to)
let request = MKDirections.Request()
request.source = MKMapItem(placemark: sourcePlacemark)
request.destination = MKMapItem(placemark: destPlacemark)
request.transportType = .automobile
let directions = MKDirections(request: request)
let response = try await directions.calculate()
guard let route = response.routes.first else {
throw NSError(domain: "No route found", code: -1)
}
return route.expectedTravelTime
}
}
@MainActor
struct TravelTimeView: View {
@State private var travelTime: String = "Calculating..."
var body: some View {
VStack {
Text(travelTime)
.font(.title2)
}
.task {
do {
// Example: Tokyo Station → Shibuya Station
let from = CLLocationCoordinate2D(latitude: 35.6811441, longitude: 139.7644865)
let to = CLLocationCoordinate2D(latitude: 35.6580382, longitude: 139.6990609)
let seconds = try await TravelTimeCalculator.calculateTravelTime(from: from, to: to)
let minutes = Int(seconds / 60)
travelTime = "🚗 Estimated \(minutes) min"
} catch {
travelTime = "❌ Route not available"
print("Route error: \(error.localizedDescription)")
}
}
}
}
Test all kind of source / dest combination…