如何使用Objective C在iphone中获取GPS坐标

shr*_*evi 2 iphone

我想从iphone获取GPS坐标并将这些GPS坐标发送到网络服务.此Web服务将从我当前的位置获取我的GPS坐标并向我发送最近的ATM的位置.现在我想分两个阶段做这个.第一阶段,我想将GPS坐标发送到网络服务,作为回报,我想要ATM位置的地址.第二阶段,我想将此ATM指向iphone应用程序中显示的MAP.

我开发了Web服务,它有2个输入参数:lat和longi.并以字符串格式返回ATM位置的地址.

从阶段1开始:请帮助我如何获得GPS坐标并将其发送到Web服务.这样我就可以在视图中以字符串格式显示地址(结果我从Web服务获得).

Fle*_*lea 24

将核心位置添加到项目中:

  1. 突出显示应用程序目标.
  2. 单击Build Phases选项卡,展开Link Binary with Libraries部分.
  3. 然后单击+按钮并选择CoreLocation框架.

头文件(.h)

注意代表的使用!

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController<CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager *locationManager;


@end
Run Code Online (Sandbox Code Playgroud)

实施文件(.m)

#import "MasterViewController.h"

@implementation MasterViewController

#pragma mark - Properties

@synthesize locationManager;

#pragma mark - Methods


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initializeMenuItems];

    if (self.locationManager == nil) 
    {
           self.locationManager = [[CLLocationManager alloc] init];
           self.locationManager.desiredAccuracy = 
        kCLLocationAccuracyNearestTenMeters;
           self.locationManager.delegate = self;
        }
    [self.locationManager startUpdatingLocation];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    // Turn off the location manager to save power.
    [self.locationManager stopUpdatingLocation];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

     // 1. Get the current location

    CLLocation *curPos = locationManager.location;

        NSString *latitude = [[NSNumber numberWithDouble:curPos.coordinate.latitude] stringValue]; 

        NSString *longitude = [[NSNumber numberWithDouble:curPos.coordinate.longitude] stringValue];

        NSLog(@"Lat: %@", latitude);
    NSLog(@"Long: %@", longitude);  
}



- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    NSLog(@"%@", @"Core location has a position.");
}
- (void) locationManager:(CLLocationManager *)manager 
        didFailWithError:(NSError *)error 
{
    NSLog(@"%@", @"Core location can't get a fix.");
}

@end
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 12

  1. 将CoreLocation框架添加到项目中
  2. 创建并设置CLLocationManager的实例(例如,设置desiredAccuracy属性)
  3. 将代理人设置为您的位置管理员
  4. 在你的委托实现2方法:-didFailWithError-didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
  5. 请致电-startUpdatingLocation您的位置经理.

在didUpdate方法中,您可以获得当前位置的所有更新,还可以检查您获得的坐标是否对您有效(检查horizo​​ntalAccuracy和timeStamp属性)