- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the distance between two geographic locations in iOS using Swift?
In this post we will learn how to calculate the distance between two geo locations.
We will show the distance between two points on a label.
To do so follow the steps below
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “FindDistance”
Step 2 − Open Main.storyboard and add two labels as shown below.
Step 3 − Attach one @IBOutlet for the bottom label. Name it distanceLabel
Step 4 − Import CoreLocation framework in ViewController
Step 5 − Add two points between which we want to find the distance as variables
var firsLocation = CLLocation(latitude:34.54545, longitude:56.64646) var secondLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
Step 6 − In viewDidLoad of the view controller add following lines.
let distance = firsLocation.distance(from: secondLocation) / 1000 distanceLabel.text = " \(String(format:"%.02f", distance)) KMs "
Here we are using the ‘distance’ function of the CoreLocation framework. This function returns distance from the point in meters. We are dividing the distance by 1000 to get the distance in Kilo meters.
Step 7 − Run the project you will see the distance on bottom labels. As shown below
- How to get the distance between two geographic locations in Android?
- How to draw a route between two locations using MapKit in Swift?
- How to get the differences between two dates in iOS?
- How to integrate PayU Money in iOS using swift?
- How to make the phone call in iOS 10 using Swift?
- How to make phone call in iOS 10 using Swift?
- How to send an attachment in email using Swift(ios)?
- How to hide the status bar in a iOS App using Swift?
- Capture picture from iOS camera using Swift
- Adding Navigation Bar programmatically iOS using Swift
- How to create a WebView in an iOS App using Swift?
- How to make an HTTP request on iOS App using Swift?
- How to Ping External host from Swift in iOS?
- How to create a Custom Dialog box on iOS App using Swift?
- How to load and display an image on iOS App using Swift?