mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Location view improvements
This commit is contained in:
@@ -7,6 +7,10 @@ import TelegramStringFormatting
|
||||
import MapKit
|
||||
|
||||
extension TelegramMediaMap {
|
||||
convenience init(coordinate: CLLocationCoordinate2D, liveBroadcastingTimeout: Int32? = nil) {
|
||||
self.init(latitude: coordinate.latitude, longitude: coordinate.longitude, geoPlace: nil, venue: nil, liveBroadcastingTimeout: liveBroadcastingTimeout)
|
||||
}
|
||||
|
||||
var coordinate: CLLocationCoordinate2D {
|
||||
return CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
|
||||
}
|
||||
@@ -76,3 +80,72 @@ func stringForDistance(strings: PresentationStrings, distance: CLLocationDistanc
|
||||
}
|
||||
return distanceFormatter.string(fromDistance: distance)
|
||||
}
|
||||
|
||||
func stringForEstimatedDuration(strings: PresentationStrings, eta: Double) -> String? {
|
||||
if eta > 0.0 && eta < 60.0 * 60.0 * 10.0 {
|
||||
var eta = max(eta, 60.0)
|
||||
let minutes = Int32(eta / 60.0) % 60
|
||||
let hours = Int32(eta / 3600.0)
|
||||
|
||||
let string: String
|
||||
if hours > 1 {
|
||||
if hours == 1 && minutes == 0 {
|
||||
string = strings.Map_ETAHours(1)
|
||||
} else {
|
||||
string = strings.Map_ETAHours(9999).replacingOccurrences(of: "9999", with: String(format: "%d:%02d", arguments: [hours, minutes]))
|
||||
}
|
||||
} else {
|
||||
string = strings.Map_ETAMinutes(minutes)
|
||||
}
|
||||
return strings.Map_DirectionsDriveEta(string).0
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func throttledUserLocation(_ userLocation: Signal<CLLocation?, NoError>) -> Signal<CLLocation?, NoError> {
|
||||
return userLocation
|
||||
|> reduceLeft(value: nil) { current, updated, emit -> CLLocation? in
|
||||
if let current = current {
|
||||
if let updated = updated {
|
||||
if updated.distance(from: current) > 250 || (updated.horizontalAccuracy < 50.0 && updated.horizontalAccuracy < current.horizontalAccuracy) {
|
||||
emit(updated)
|
||||
return updated
|
||||
} else {
|
||||
return current
|
||||
}
|
||||
} else {
|
||||
return current
|
||||
}
|
||||
} else {
|
||||
if let updated = updated, updated.horizontalAccuracy > 0.0 {
|
||||
emit(updated)
|
||||
return updated
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func driveEta(coordinate: CLLocationCoordinate2D) -> Signal<Double?, NoError> {
|
||||
return Signal { subscriber in
|
||||
let destinationPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
|
||||
let destination = MKMapItem(placemark: destinationPlacemark)
|
||||
|
||||
let request = MKDirections.Request()
|
||||
request.source = MKMapItem.forCurrentLocation()
|
||||
request.destination = destination
|
||||
request.transportType = .automobile
|
||||
request.requestsAlternateRoutes = false
|
||||
|
||||
let directions = MKDirections(request: request)
|
||||
directions.calculateETA { response, error in
|
||||
subscriber.putNext(response?.expectedTravelTime)
|
||||
subscriber.putCompletion()
|
||||
}
|
||||
return ActionDisposable {
|
||||
directions.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user