1

I am working in 1 iphone app where we need to show the direction from 1 place to other place That is from current location in which direction (N or E or W or S or NE or NW or SE or SW ) the destination is . I know how to get the directions from 1 place to another place but I am searching for showing the direction . Is my problem clear ? If any one know how to do it , Pleae help me Thanks every one!

ratnasomu
  • 174
  • 1
  • 1
  • 14

2 Answers2

3

Do you know the coordinates of the two locations?

Look at CLLocationCoordinate2D:

latitude The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.

longitude The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian.

Latitude is therefore quite simple, if the destination's latitude is less than the current location's latitude then it lies to the north, if it is greater than the current location's latitude then it lies to the south.

latitudinal_distance = destination.latitude - origin.latitude

Longitude is slightly more complex as you should consider that a destination three quarters of the way around the Earth to the east is probably better expressed as being to the west. Again compare the longitude values, consider the distance both east and west, handle crossing the meridian, and choose the shorter distance.

distance_east = (origin.longitude > 0 && destination.longitude < 0) ? 180 - origin.longitude + destination.longitude - -180 : destination.longitude - origin.longitude; if (distance_east < 0) distance_east += 360

distance_west = (origin.longitude < 0 && destination.longitude > 0) ? -180 - origin.longitude - 180 - destination.longitude : origin.longitude - destination.longitude; if (distance_west < 0) distance_west += 360

longitudinal_distance = min(distance_east, distance_west)

Once you know how many degrees of latitude and longitude separate your two points you can calculate a heading to your destination and decide which heading should be show as which compass points. On a compass with only four points (N, E, S, W) each point would cover 90 degrees. On a compass with 8 points each point would cover 45 degrees. Hopefully you get the idea.

heading = arctan(longitudinal_distance / latitudinal_distance) if (heading >= -45 || heading < 45) return 'N'; else if (heading >= 45 && heading < 135) return 'E'; else if ...

It's late here and I'm not testing those expressions so if they seem useful please make sure you understand and test them instead of trying to apply them blindly. The odds that I transposed a sign or coordinate pair are unfortunately high.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • Hi Jonah , Thanks for Your response and answer , I have tried the code and using heading value I wrote like below : if (heading >= -45 || heading < 0) return @"North"; else if (heading >= 0 && heading < 45) return @"East"; else if(heading>=45 && heading < 90) return @"South"; else if(heading>=90 && heading<135) return @"West";else if(heading>=135 && heading < 180) return @"North East"; else if (heading >= 180 || heading < 225) return @"North West"; else if (heading >= 225 && heading < 270) return @"South East"; else if(heading>=270 && heading < 315) return @"South West"; is this correct – ratnasomu Jan 10 '11 at 08:00
  • Did you try it? You seem to be assuming that heading will range from 0 to 360 which does not match with the assumptions I made in my example. Take a look at how those values are calculated and consider what the possible outputs are. – Jonah Jan 19 '11 at 00:58
  • Yes I assumed like u said . is that wrong ? can u tell me how I need to do? – ratnasomu Jan 20 '11 at 08:56
  • I will try to help if you still have specific questions but I think I have given you everything you need to solve this problem. – Jonah Jan 20 '11 at 09:11
  • Yes but I guess my code is wrong .. I understand your answer as we should take 0 to 360 to check the direction. can u help me by seeing my code above Thank you – ratnasomu Jan 20 '11 at 11:23
0

Take a look at the magnetometer / core location sample by Apple: Teslameter

BojanG
  • 1,872
  • 1
  • 15
  • 23
  • The OP wants to know the direction between two points on a map and you're suggesting to read the compass. Those are two totally different things. – Gabe Jan 08 '11 at 08:29
  • Thanks BojanG , is it not possible with Google maps api ? – ratnasomu Jan 08 '11 at 08:30