2

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)

img

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.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Putte
  • 328
  • 2
  • 17
  • Add the distances between each point and the next – Paulw11 Mar 12 '19 at 11:13
  • @Paulw11 How can I do that? – Putte Mar 12 '19 at 11:13
  • 3
    You have an array of locations. Iterate through the array, finding the distance between each point and the next. Total those distances – Paulw11 Mar 12 '19 at 11:14
  • @Paulw11 Do you mind adding a code example? I've no idea where to start lol – Putte Mar 12 '19 at 11:25
  • 2
    There is a `distance(from:)` method in the CLLocation class – Joakim Danielson Mar 12 '19 at 11:30
  • @JoakimDanielson I'm using the distance(to:) atm. Why would it change if I use (from:) – Putte Mar 12 '19 at 11:32
  • 1
    Not sure what distance(to:) is but if it works for you then use that instead otherwise as I said the other function is in the CLLocation class. – Joakim Danielson Mar 12 '19 at 11:36
  • 1
    @Putte, this `distance(to:)` you're using is actually method of `Array.Index` type, which is `Int` right now. [And this function just returns difference between two indexes](https://developer.apple.com/documentation/swift/int/1641606-distance). For `.startIndex` and `.endIndex` of `Array` it would be just length of array. – user28434'mstep Mar 12 '19 at 11:56

1 Answers1

3
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)
    }
}
El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • Instead of "distanceTapped" I can add that code in my play button? to make it automatic? Thanks a lot for your input, Ill look into this – Putte Mar 12 '19 at 11:53
  • 1
    Yes. You have to come with locations, an array of CLLocationCoordinate2D, for yourself, as per your code, though. – El Tomato Mar 12 '19 at 12:09
  • Seems like its working fine! The result is now: 4.5668457328325329. (It's 4 meter). How can I remove the ".5668457328325329"? Do you know? I've tried to /1000 etc, but not really working correctly – Putte Mar 12 '19 at 12:20
  • Use modf. https://stackoverflow.com/questions/31396301/getting-the-decimal-part-of-a-double-in-swift – El Tomato Mar 12 '19 at 12:22
  • @Putte, are you sure you want to round it down? Because it's closer to `5` than to `4`. – user28434'mstep Mar 12 '19 at 12:28
  • @user28434 Oh well, that's true. I just wanna hide the decimals, that's all. :) – Putte Mar 12 '19 at 12:29