问题描述
我一直试图学习如何在Objective-C中实现iPhone的位置.目前,我有几个文件:
So ive been trying to learn how to implement iPhone location in Objective-C. Currently I have several files:
locator.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface locator : NSObject
- (void)locationManager:(CLLocationManager *)manager;
@end
locator.m
#import "locator.h"
#import <CoreLocation/CoreLocation.h>
@implementation locator
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CLLocationDegrees latitude = newLocation.coordinate.latitude;
CLLocationDegrees longitude = newLocation.coordinate.longitude;
}
@end
viewController.h
#import <UIKit/UIKit.h>
#import "locator.h"
@interface ViewController : UIViewController
@end
viewController.m
#import "ViewController.h"
#import "locator.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; // Set your controller as a <CLLocationManagerDelegate>.
[locationManager startUpdatingLocation];
[super viewDidLoad];
}
@end
我肯定我自己有时犯了一个重大错误,但是我很困惑,根本不了解它是什么.我在尝试运行此程序时遇到2个主要错误.
Im sure ive made a major error sometimes but im quite confused and dont really understand what it is. Im getting 2 major errors when trying to run this.
推荐答案
通常,我将CLLocationManager设置为类变量,如下所示:
Usually I would make the CLLocationManager a class variable like so:
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager
@end
然后您将可以拨打电话:
Then you will be able to call:
[self.locationManager stopUpdatingLocation];
在您想要的时候.另外,您还需要实现:
when you desire. Also you need to implement:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
}
在您的ViewController中,以接收具有位置数据的委托回调.
In your viewcontroller to receive the delegate callback that has the location data.
这篇关于在Objective-C中实现iPhone位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!