[英]Implementing iPhone Location in Objective-C
So ive been trying to learn how to implement iPhone location in Objective-C. 因此,我一直试图学习如何在Objective-C中实现iPhone的位置。 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 viewController.h
#import <UIKit/UIKit.h>
#import "locator.h"
@interface ViewController : UIViewController
@end
viewController.m 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
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.
我在尝试运行时遇到2个主要错误。
@interface ViewController : UIViewController
@end
Must become : 必须成为:
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@end
It should work now. 现在应该可以工作了。
EDIT : Do not use your own locator
class if you just want to get the iDevice coordinates, it's faster to use this directly in your viewController. 编辑 :如果只想获取iDevice坐标,请不要使用自己的
locator
类,直接在viewController中使用它会更快。
Because if you want to do this with your own class you have to : 因为如果您想在自己的班上这样做,则必须:
And it's easier to explain :) 而且更容易解释:)
Hope this helps. 希望这可以帮助。
Usually I would make the CLLocationManager a class variable like so: 通常,我会将CLLocationManager设为类变量,如下所示:
@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 {
}
In your viewcontroller to receive the delegate callback that has the location data. 在您的ViewController中,接收具有位置数据的委托回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.