66

Possible Duplicate:
GPS coordinates in degrees to calculate distances

How to Calculate distance between two place using latitude-longitude in gmap for iPhone? Calculate distance between two place using latitude-longitude in gmap for iPhone

Community
  • 1
  • 1
Durga
  • 933
  • 1
  • 9
  • 12
  • If you have the latitude and longitude of two points, you can calculate the distance between them using the [haversine formula](http://en.wikipedia.org/wiki/Haversine_formula). – Katfish Aug 24 '11 at 12:44

2 Answers2

266

You have to use Core Location Framework. So don't forget to Add & import Core Location Framework.

Objective-C

 #import <CoreLocation/CoreLocation.h>

 CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];

 CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];

 CLLocationDistance distance = [locA distanceFromLocation:locB];

 //distance in Meters

 //1 meter == 100 centimeter

 //1 meter == 3.280 feet

 //1 square meter == 10.76 square feet


 [locA release];

 [locB release];

Swift

import CoreLocation

let locA = CLLocation(latitude: lat1, longitude: long1)

let locB = CLLocation(latitude: lat2, longitude: long2)

let distance = locA.distance(from: locB)
Cœur
  • 37,241
  • 25
  • 195
  • 267
11

if you have the location parameter, create 2 CLLocations and use [location1 distanceFromLocation:location2] method.

Nithin
  • 6,435
  • 5
  • 43
  • 56