Sorting cells by distance - ios

So I'm trying to sort the cells by distance calculated. I have a struct
struct Sal {
var name:String
var coordinatesLat: CLLocationDegrees
var coordinatesLong: CLLocationDegrees
//var distance?
}
in scene A
func distanceFromLocation(location: CLLocation) -> CLLocationDistance {
// Find lat and long coordinates of current location
let getLat = self.location.latitude
let getLon = self.location.longitude
let currentLocation = CLLocation(latitude: getLat, longitude: getLon)
// Calculate distance from current location to sal
let distance = currentLocation.distanceFromLocation(location)
return distance
}
scene B
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SalCustomCell
let name = sals[indexPath.row].name
cell.name.text = name
// Calculate distance between current location and sal
let salLocation = CLLocation(latitude: sals[indexPath.row].coordinatesLat, longitude: sals[indexPath.row].coordinatesLong)
// Display distance in cell.
let distanceM = viewController.distanceFromLocation(salLocation)
let distanceKm = round(distanceM/1000)
cell.distance.text = String(distanceKm) + "km"
return cell
}
I call the functions from scene A to display the distance in Km in scene B. I'm trying to sort them by distance displayed in cell but i'm not to sure on how i would sort them by. If it was name i could just do
sals.sortInPlace ({ $0.name < $1.name })
but I can't do
sals.sortInPlace ({ $0.distance < $1.distance })
because it's not apart of the struct. Do I need to make it apart of the struct? If so how?
I also tried to create an empty array and append the distance into the array to sort that way
var distance = [Double]()
self.distance.append(distanceKm) // inside cellForRowAtIndexPath
distance.sortInPlace ({ $0 < $1 })
But this didn't work out

Rewrite Sal so that it contains your distance calculation function, like this:
struct Sal {
var name:String
var coordinatesLat: CLLocationDegrees
var coordinatesLong: CLLocationDegrees
func distanceFromLocation(location: CLLocation) -> CLLocationDistance {
// ...
}
}
Now your array of Sal has a Sal feature that you can sort on.

Related

Calculating trip distance core location swift

I have an application where I calculate distance travelled like the Uber application. When a driver starts a trip, the location begins to change even though a start point has been specified in the search for a ride, a driver could decide to pass an alternative route or pass long places and routes because he/ she does not know the shortest route, how then do I calculate the total distance.
The starting location is the location the driver hits start button
The end location is the location the driver hits stop button
this is my code so far
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last!
endTrip(locations.last)
if !hasSetInitialLocation {
let camera = GMSCameraPosition.camera(withTarget: lastLocation!.coordinate, zoom: 17)
self.mapView.animate(to: camera)
hasSetInitialLocation = true
endTrip(lastLocation)
MqttManager.instance.connectToServer()
}
}
func endTrip(endLoaction: CLLocation) {
guard let statusChange = source.getStatusChange() else{return}
var distanceTraveled: Double = 0.0
let initialLocation = CLLocation(latitude: (statusChange.meta?.location?.lat)!, longitude: (statusChange.meta?.location?.lng)!)
let distance = initialLocation.distance(from: endLoaction)
distanceTraveled += distance
let distanceInKM = Utility.convertCLLocationDistanceToKiloMeters(targetDistance: distanceTraveled)
}
How can i calculate the distance to reflect the total distance moved by the driver since there could be a change in route from the proposed start point and end point.
The driver hits a button called start trip, I want to get the distance from that moment till the moment he hits the button end trip
this implementation could be got from a similar working code like these but the only difference is that their is a start button which passes the coordinates at that point and a stop coordinate which is the end of the coordinate.
enum DistanceValue: Int {
case meters, miles
}
func calculateDistanceBetweenLocations(_ firstLocation: CLLocation, secondLocation: CLLocation, valueType: DistanceValue) -> Double {
var distance = 0.0
let meters = firstLocation.distance(from: secondLocation)
distance += meters
switch valueType {
case .meters:
return distance
case .miles:
let miles = distance
return miles
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if startLocation == nil {
startLocation = locations.first
} else if let location = locations.last {
runDistance += lastLocation.distance(from: location)
let calc = calculateDistanceBetweenLocations(lastLocation, secondLocation: location, valueType: .meters)
print("TOTAL LOC 1 \(calc)")
print("TOTAL LOC 2 \(runDistance)")
}
lastLocation = locations.last
}
as shown in my print statements print("TOTAL LOC 1 \(calc)")
print("TOTAL LOC 2 \(runDistance)") how can I make
calc the same with runDistance
here is what is printed in the console
TOTAL LOC 10.29331530774379
TOTAL LOC 2 10.29331530774379
TOTAL LOC 2.2655118031831587
TOTAL LOC 2 12.558827110926948
If you get the distance like this using the first and last coordinate it always returns the wrong value because it can't identify the actual traveling path.
I did resolve the same issue with using the following code.
use GoogleMaps
> pod 'GoogleMaps'
Make the coordinates array while the driver is moving on a route.
var arr = [Any]()
// Driving lat long co-ordinateds continues add in this array according to your expectation either update location or perticuler time duration.
// make GMSMutablePath of your co-ordinates
let path = GMSMutablePath()
for obj in arr{
print(obj)
if let lat = (obj as? NSDictionary)?.value(forKey: PARAMETERS.LET) as? String{
path.addLatitude(Double(lat)!, longitude: Double(((obj as? NSDictionary)?.value(forKey: PARAMETERS.LONG) as? String)!)!)
}
}
print(path) // Here is your traveling path
let km = GMSGeometryLength(path)
print(km) // your total traveling distance.
I did it in this app and it's working fine.
Hope it will helps you :)
OR without GoogleMaps
You have to come with locations, an array of CLLocationCoordinate2D, for yourself, as per your code, though.
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// MARK: - Variables
let locationManager = CLLocationManager()
// MARK: - IBOutlet
#IBOutlet weak var mapView: MKMapView!
// MARK: - IBAction
#IBAction func distanceTapped(_ sender: UIBarButtonItem) {
let locations: [CLLocationCoordinate2D] = [...]
var total: Double = 0.0
for i in 0..<locations.count - 1 {
let start = locations[i]
let end = locations[i + 1]
let distance = getDistance(from: start, to: end)
total += distance
}
print(total)
}
func getDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
// By Aviel Gross
// https://stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
return from.distance(from: to)
}
}
Output
A simple function to calculate distance (in meters) given an array of CLLocationCoordinate2D. Uses reduce instead of array iteration.
func computeDistance(from points: [CLLocationCoordinate2D]) -> Double {
guard let first = points.first else { return 0.0 }
var prevPoint = first
return points.reduce(0.0) { (count, point) -> Double in
let newCount = count + CLLocation(latitude: prevPoint.latitude, longitude: prevPoint.longitude).distance(
from: CLLocation(latitude: point.latitude, longitude: point.longitude))
prevPoint = point
return newCount
}
}
I like to use an extension for that
extension Array where Element: CLLocation {
var distance: Double {
guard count > 1 else { return 0 }
var previous = self[0]
return reduce(0) { (result, location) -> Double in
let distance = location.distance(from: previous)
previous = location
return result + distance
}
}
}
Usage:
locations.distance

Count distance on polyline swift

I've created a map where you can press a start button. The application will then zoom in to your current location, and update the coordinate every 10 second and insert into an array of coordinates. Once I press the stop button, I've a polyline which draws lines between all coordinates. (Like the image below)
So my question is now:
How can I count the distance the polyline was drawn?
//Draw polyline on the map
let aPolyLine = MKPolyline(coordinates: self.locations, count: self.locations.count)
//Adding polyline to mapview
self.mapView.addOverlay(aPolyLine)
let startResult = self.locations.startIndex
let stopResult = self.locations.endIndex
//Retrieve distance and convert into kilometers
let distance = startResult.distance(to: stopResult)
let result = Double(distance) / 1000
let y = Double(round(10 * result)) / 10
self.KiloMeters.text = String(y) + " km"
My guess is that I cannot use startResult.distnace(to: stopResult) because, if I walk in a circle, the kilometer will show 0? right? I'm not sure, but it still dosent work. Nothing is showing when using the code like I've.
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// MARK: - Variables
let locationManager = CLLocationManager()
// MARK: - IBOutlet
#IBOutlet weak var mapView: MKMapView!
// MARK: - IBAction
#IBAction func distanceTapped(_ sender: UIBarButtonItem) {
let locations: [CLLocationCoordinate2D] = [...]
var total: Double = 0.0
for i in 0..<locations.count - 1 {
let start = locations[i]
let end = locations[i + 1]
let distance = getDistance(from: start, to: end)
total += distance
}
print(total)
}
func getDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
// By Aviel Gross
// https://stackoverflow.com/questions/11077425/finding-distance-between-cllocationcoordinate2d-points
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
return from.distance(from: to)
}
}

(Swift) set a 1km radius within my location MapKit

I'm working on an App where I get the user location and some custom pins (Restaurants) that I added. What I want to do is set radius (between 5km and 10km) distance between the user and the restaurants and then show those that are within the area.
My question is, how can I set that radius and get the locations within it
Best regards!
You can do some distance calculations to determine what pins to show.
let pins = Array<MKPointAnnotation>() // This is an empty array, so just use your array of locations or add to this.
if let currentLocation = locationManager.location?.coordinate {
for pin in pins {
let locationMapPoint = MKMapPointForCoordinate(currentLocation)
let pinMapPoint = MKMapPointForCoordinate(pin.coordinate)
let distance = MKMetersBetweenMapPoints(locationMapPoint, pinMapPoint)
if distance >= 5000 && distance <= 10000 {
self.map.addAnnotation(pin)
}
}
}
If you wanted something tidier, you could also do this.
let pins = Array<MKPointAnnotation>()
if let currentLocation = locationManager.location?.coordinate {
let filtered = pins.filter { $0.coordinate.distance(to: currentLocation) >= 5000 && $0.coordinate.distance(to: currentLocation) <= 10000 }
self.map.addAnnotations(filtered)
}
You'll need this extension aswell.
extension CLLocationCoordinate2D {
func distance(to coordinate: CLLocationCoordinate2D) -> Double {
return MKMetersBetweenMapPoints(MKMapPointForCoordinate(self), MKMapPointForCoordinate(coordinate))
}
}

How to make Text Show distance from The user current location to a certain Map annotation

Just wondering How to do that , really would like this in my custom cell in the table view in my app...
Will appreciate any help thank you !
You can calculate the distance between two CLLocation objects with the distanceFromLocation method:
let newYork = CLLocation(latitude: 40.725530, longitude: -73.996738)
let sanFrancisco = CLLocation(latitude: 37.768, longitude: -122.441)
let distanceInMeters = newYork.distanceFromLocation(sanFrancisco)
With an MKMapView object and an MKAnnotationView object, you can calculate the distance between the user's current location and the annotation as follows:
if let userLocation = mapView.userLocation.location, annotation = annotationView.annotation {
// Calculate the distance from the user to the annotation
let annotationLocation = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
let distanceFromUserToAnnotationInMeters = userLocation.distanceFromLocation(annotationLocation)
...
}
The following function uses the NSNumberFormatter class to format a distance in meters or kilometres (if the number of meters is more than 1000):
func formatDistance(distanceInMeters: CLLocationDistance) -> String? {
// Set up a number formatter with two decimal places
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle
numberFormatter.maximumFractionDigits = 2
// Display as kilometers if the distance is more than 1000 meters
let distanceToFormat: CLLocationDistance = distanceInMeters > 1000 ? distanceInMeters/1000.0 : distanceInMeters
let units = distanceInMeters > 1000 ? "Km" : "m"
// Format the distance
if let formattedDistance = numberFormatter.stringFromNumber(distanceToFormat) {
return "\(formattedDistance)\(units)"
} else {
return nil
}
}
Putting all this together gives us the following:
if let userLocation = mapView.userLocation.location, annotation = annotationView.annotation {
// Calculate the distance from the user to the annotation
let annotationLocation = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
let distanceFromUserToAnnotationInMeters = userLocation.distanceFromLocation(annotationLocation)
if let formattedDistance = formatDistance(distanceFromUserToAnnotationInMeters) {
// Now set the vaue of your label to formattedDistance
}
}

Remove Object From Array Using Enumeration

I have an array of dictionaries. Each dictionary contains latitude and longitude so I'm getting the distance of each item from the current user location. If the distance in miles is greater than 20, that particular dictionary should be removed from the array. If the dictionary is not removed from the array, an annotation is created and added to an annotation array which is then used to add annotations to a map once the enumeration is finished. I'm only getting one annotation added when I should be getting three so I know I'm doing something wrong in my enumeration.
func checkDistanceAndAddPins() {
for gym in gyms {
var index = 0
let gymLatitude = gym["latitude"]!!.doubleValue
let gymLongitude = gym["longitude"]!!.doubleValue
let gymLocation = CLLocation(latitude: gymLatitude, longitude: gymLongitude)
let distance = gymLocation.distanceFromLocation(myLocation!)
let distanceInMeters = NSNumber(double: distance)
let metersDouble = distanceInMeters.doubleValue
let miles = metersDouble * 0.00062137
if miles > maxDistance {
gyms.removeAtIndex(index)
} else {
let location = CLLocationCoordinate2D(latitude: gymLatitude, longitude: gymLongitude)
gymAnnotation.title = gym["Name"] as? String
gymAnnotation.subtitle = gym["Address"] as? String
gymAnnotation.coordinate = location
gymAnnotation.gymPhoneNumber = gym["Phone"] as? String
if let website = gym["Website"] as? String {
gymAnnotation.gymWebsite = website
}
gymLocations.append(gymAnnotation)
}
index += 1
}
dispatch_async(dispatch_get_main_queue()) {
self.gymMap.addAnnotations(self.gymLocations)
}
}

Resources