ios8 - how to get the distance between to points on earth in Swift? -
i used 2 methods .. apple's 1
@ibaction func getdistancepressed(sender: anyobject) { distancelabel.text = "\(location_2.distancefromlocation(location_1))m" // meters }
and mine
@ibaction func getdistancepressed(sender: anyobject) { let lat1 = location_1.coordinate.latitude let lon1 = location_1.coordinate.longitude let lat2 = location_2.coordinate.latitude let lon2 = location_2.coordinate.longitude distancelabel.text = "\(getdistancefromlatloninmeter(lat1, lon1: lon1, lat2: lat2, lon2: lon2))m" } func getdistancefromlatloninmeter(lat1 : double ,lon1 : double ,lat2 : double ,lon2 : double) -> double{ var earthradius : double = 6372797.560856 //inmeter var deltalat : double = deg2rad(lat2 - lat1) var deltalon : double = deg2rad(lon2 - lon1) var latitudeh = sin(deltalat * 0.5) latitudeh *= latitudeh var lontitudeh = sin(deltalon * 0.5) lontitudeh *= lontitudeh var tmp = cos(deg2rad(lat2)) * cos(deg2rad(lat1)); return earthradius * 2.0 * asin(sqrt(latitudeh + tmp*lontitudeh)) } func deg2rad(deg : double) -> double { return deg * 0.017453292519943295769236907684886 }
both giving me same value , , both wrong .. when move iphone around (1 meter) gives me 11 meters , when move 3 meters gives me 20 , , on . don't know problem . correct method thing .how distance??
for example --- >
firstlatitude - > 24.4348484485008
firstlongitude - > 39.6552843828735
lastlatitude - > 24.4349537442103
lastlongitude - > 39.6552204902715
it gives distance - > 13.379 m
it should 0.7 m (in real)
Comments
Post a Comment