3

我的应用程序有一个问题我想知道从当前纬度和经度到纬度和经度的方向

-(void)showDirection
{
 CGFloat latitude = Lat;
 if (latitude < 0) {
    latitude = -latitude;
    strDirection = @"S";
 } else {
    strDirection = @"N";
 }
 // Longitude
 CGFloat longitude = Long;
 if (longitude < 0) {
    longitude = -longitude;
    strDirection = @"W";
 } else {
    strDirection = @"E";
 }
 NSLog(@"direc %@",strDirection);
}
4

2 回答 2

8

试试下面的代码:

在添加初始化 CLLocationManager 对象并导入 CLLocationManager 头文件后,首先在项目CoreLocation.Framework中添加框架。

你的视图控制器.h

#import <CoreLocation/CoreLocation.h>
@interface Yourviewcontroller :UIViewController <CLLocationManagerDelegate>
{
      CLLocationManager *locationManager;
} 

你的视图控制器.m

locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
//Start the compass updates.
[locationManager startUpdatingHeading];

添加获取当前方向的功能

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    float mHeading = newHeading.magneticHeading;
    if ((mHeading >= 339) || (mHeading <= 22)) {
        //[direction setText:@"N"]; 

    }else if ((mHeading > 23) && (mHeading <= 68)) {
        //[direction setText:@"NE"];    

    }else if ((mHeading > 69) && (mHeading <= 113)) {
        //[direction setText:@"E"]; 

    }else if ((mHeading > 114) && (mHeading <= 158)) {
        //[direction setText:@"SE"];

    }else if ((mHeading > 159) && (mHeading <= 203)) {
        //[direction setText:@"S"]; 

    }else if ((mHeading > 204) && (mHeading <= 248)) {
        //[direction setText:@"SW"];    

    }else if ((mHeading > 249) && (mHeading <= 293)) {
       // [direction setText:@"W"];

    }else if ((mHeading > 294) && (mHeading <= 338)) {
       // [direction setText:@"NW"];

     }
}

注意:指南针仅适用于真正的 iphone 设备,不适用于 iphone 模拟器..!

于 2012-04-20T11:47:34.597 回答
-4

你可以试试LocateMe。它将为您提供用户当前位置的经纬度。
或者您可以尝试查看本教程

希望这可以帮助!

于 2012-04-20T11:48:57.370 回答