JSRUN 用代码说话

定位操作

编辑教程

定位操作

在IOS中通过CoreLocation定位,可以获取到用户当前位置,同时能得到装置移动信息。

实例步骤

1、创建一个简单的View based application(视图应用程序)。

2、择项目文件,然后选择目标,然后添加CoreLocation.framework,如下所示

iOS定位操作

3、在ViewController.xib中添加两个标签,创建ibOutlet名为latitudeLabel和longtitudeLabel的标签

4、现在通过选择" File-> New -> File... -> "选择Objective C class 并单击下一步

5、把"sub class of"作为NSObject,将类命名为LocationHandler

6、选择创建

7、更新LocationHandler.h,如下所示

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

@protocol LocationHandlerDelegate <NSObject>

@required
-(void) didUpdateToLocation:(CLLocation\*)newLocation 
 fromLocation:(CLLocation\*)oldLocation;
@end

@interface LocationHandler : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager \*locationManager;
}
@property(nonatomic,strong) id<LocationHandlerDelegate> delegate;

+(id)getSharedInstance;
-(void)startUpdating;
-(void) stopUpdating;

@end

8、更新LocationHandler.m,如下所示

#import "LocationHandler.h"
static LocationHandler \*DefaultManager = nil;

@interface LocationHandler()

-(void)initiate;

@end

@implementation LocationHandler

+(id)getSharedInstance{
    if (!DefaultManager) {
        DefaultManager = \[\[self allocWithZone:NULL\]init\];
        \[DefaultManager initiate\];
    }
    return DefaultManager;
}
-(void)initiate{
    locationManager = \[\[CLLocationManager alloc\]init\];
    locationManager.delegate = self;
}

-(void)startUpdating{
    \[locationManager startUpdatingLocation\];
}

-(void) stopUpdating{
    \[locationManager stopUpdatingLocation\];
}
-(void)locationManager:(CLLocationManager \*)manager didUpdateToLocation:
 (CLLocation \*)newLocation fromLocation:(CLLocation \*)oldLocation{
    if (\[self.delegate respondsToSelector:@selector
    (didUpdateToLocation:fromLocation:)\]) 
    {
        \[self.delegate didUpdateToLocation:oldLocation 
        fromLocation:newLocation\];

    }
}

@end

9、更新ViewController.h,如下所示

#import <UIKit/UIKit.h>
#import "LocationHandler.h"
@interface ViewController : UIViewController<LocationHandlerDelegate>
{
    IBOutlet UILabel \*latitudeLabel;
    IBOutlet UILabel \*longitudeLabel;
}
@end

10、更新ViewController.m,如下所示

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    \[super viewDidLoad\];
    \[\[LocationHandler getSharedInstance\]setDelegate:self\];
    \[\[LocationHandler getSharedInstance\]startUpdating\];
}

- (void)didReceiveMemoryWarning
{
    \[super didReceiveMemoryWarning\];
    // Dispose of any resources that can be recreated.
}

-(void)didUpdateToLocation:(CLLocation \*)newLocation 
 fromLocation:(CLLocation \*)oldLocation{
    \[latitudeLabel setText:\[NSString stringWithFormat:
    @"Latitude: %f",newLocation.coordinate.latitude\]\];
    \[longitudeLabel setText:\[NSString stringWithFormat:
    @"Longitude: %f",newLocation.coordinate.longitude\]\];

}

@end

输出

当我们运行该应用程序,会得到如下的输出:

iOS定位操作

JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。 大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
支付宝
9.99
无法付款,请点击这里
金额: 0
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟