iPhone - How do i get direction with degree based location

余生长醉 提交于 2019-11-30 21:03:18

问题


First I've implemented location manager functions in my class and whict are working fine, and gives me the current location. From that location I got how to get location degrees from here. but I'm not able to get the direction (i.e. North, South, East, West) I've referred this too. I want the location that I'm getting to be displayed in degrees with direction format like this. i.e. location manager gives me +37.33019332,-122.02298792 and i want something like 37° 19' 49"N, -122° 1' 23"E. I'm getting all things just don't know how to get the last "N" and "E". If i use CLLocation.course for this I'm getting my direction course. Any help would be appreciated.


回答1:


This is actually very simple. Latitudes begin at 0° at the equator with the north pole being 90.0 and the south pole being -90.0. Basically, if the latitude is between 0 and 90, you're in the northern hemisphere and the southern hemisphere for latitude between 0 and -90.

Longitude basically works the same way. 0° refers to the prime meridian which is the imaginary line that runs through Greenwich, England and a part of Africa. A positive longitude up to 180° refers to locations east of the prime meridian, while negative longitudes refer to areas west of the prime meridian up to 180°.




回答2:


Use this code and put CLLocationManagerDelegate at .h file

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{             
    updatedHeading = newHeading.magneticHeading;
    float headingFloat = 0 - newHeading.magneticHeading;

    rotateImg.transform = CGAffineTransformMakeRotation(headingFloat*radianConst);    
    float value = updatedHeading;
    if(value >= 0 && value < 23)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° N",value];
    }
    else if(value >=23 && value < 68)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° NE",value];
    }
    else if(value >=68 && value < 113)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° E",value];
    }
    else if(value >=113 && value < 185)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° SE",value];
    }
    else if(value >=185 && value < 203)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° S",value];
    }
    else if(value >=203 && value < 249)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° SE",value];
    }
    else if(value >=249 && value < 293)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° W",value];
    }
    else if(value >=293 && value < 350)
    {
        compassFault.text = [NSString stringWithFormat:@"%f° NW",value];
    }
  }


来源:https://stackoverflow.com/questions/11624481/iphone-how-do-i-get-direction-with-degree-based-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!