我下载了this教程的源代码,并对其进行了一些操作。 一切正常。
现在我想创建自己的项目来实现位置和标题。但写完之后,它不起作用。我甚至没有在顶部看到GPS指示。
当我运行其他应用程序时,它很好。 Xcode不会给出任何错误或警告。
我的代码: (它没有改变AppDelegate中的任何内容。)
LocationAndHeadingTestViewController.h
#import <UIKit/UIKit.h>
#import "LocationController.h"
@interface LocationAndHeadingTestViewController : UIViewController <LocationControllerDelegate>
{
LocationController *_locationController;
IBOutlet UILabel *_errorLabel;
//Location
IBOutlet UILabel *_locationTimeLabel;
IBOutlet UILabel *_latitudeLabel;
IBOutlet UILabel *_longitudeLabel;
IBOutlet UILabel *_altitudeLabel;
//Heading
IBOutlet UILabel *_headingTimeLabel;
IBOutlet UILabel *_trueHeadingLabel;
IBOutlet UILabel *_magneticHeadingLabel;
IBOutlet UILabel *_headingAccuracyLabel;
IBOutlet UIImageView *_compass;
}
@property (nonatomic, retain) LocationController *locationController;
@end
LocationAndHeadingTestViewController.m
#import "LocationAndHeadingTestViewController.h"
@implementation LocationAndHeadingTestViewController
@synthesize locationController = _locationController;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
_locationController = [LocationController new];
_locationController.delegate = self;
[_locationController.locationManager startUpdatingLocation];
[_locationController.locationManager startUpdatingHeading];
}
- (void)locationUpdate:(CLLocation *)location
{
[_locationTimeLabel setText:[NSString stringWithFormat:@"%@", location.timestamp]];
[_latitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.latitude]];
[_longitudeLabel setText:[NSString stringWithFormat:@"%f", location.coordinate.longitude]];
[_altitudeLabel setText:[NSString stringWithFormat:@"%f", [location altitude]]];
}
- (void)headingUpdate:(CLHeading *)heading
{
[_headingTimeLabel setText:[NSString stringWithFormat:@"%@", heading.timestamp]];
[_trueHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.trueHeading]];
[_magneticHeadingLabel setText:[NSString stringWithFormat:@"%f", heading.magneticHeading]];
[_headingAccuracyLabel setText:[NSString stringWithFormat:@"%f", heading.headingAccuracy]];
}
- (void)locationError:(NSError *)error
{
_errorLabel.text = [error description];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dealloc
{
[_locationController release];
[super dealloc];
}
@end
LocationController.h
#import <CoreLocation/CoreLocation.h>
@protocol LocationControllerDelegate
@required
- (void)locationUpdate:(CLLocation *)location;
- (void)headingUpdate:(CLHeading *)heading;
- (void)locationError:(NSError *)error;
@end
@interface LocationController : NSObject <CLLocationManagerDelegate>
{
CLLocationManager *_locationManager;
id _delegate;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, assign) id delegate;
@end
LocationController.m
#import <CoreLocation/CoreLocation.h>
#import "LocationController.h"
@implementation LocationController
@synthesize locationManager = _locationManager, delegate = _delegate;
- (id)init
{
if(self = [super init])
{
_locationManager = [[CLLocationManager new] autorelease];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)])
{
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)])
{
[_delegate locationError:error];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if([self.delegate conformsToProtocol:@protocol(LocationControllerDelegate)])
{
[_delegate headingUpdate:newHeading];
}
}
- (void)dealloc
{
[_locationManager release];
[super dealloc];
}
@end
这很简单,但我认为我真的忽视了一些事情。
聚苯乙烯。我会将id委托更改为id&lt; ..&gt;代表
答案 0 :(得分:1)
您不应该自动释放CLLocationManager对象:
_locationManager = [[CLLocationManager new] autorelease];
这是一个“保留”财产。您需要删除自动释放,您将在dealloc中释放它。你只能发布一次。
如果您使用setter分配了它,那么setter会为您保留它。这样就可以了:
self.locationManager = [[CLLocationManager new] autorelease];
答案 1 :(得分:0)
您的应用是否已在iPhone设置中启用了位置服务?
位置服务(全局访问)可以打开,但位置服务设置中的特定应用也可能同时被拒绝访问位置。