问题
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!
回答1:
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.
回答2:
Take a look at the magnetometer / core location sample by Apple: Teslameter
来源:https://stackoverflow.com/questions/4632966/using-lattitude-and-longitude-of-the-current-location-and-destination-location