尝试获取当前位置,但从不调用LocationManager委托

时间:2013-06-22 16:39:33

标签: ios delegates location mapkit cllocationmanager

这是我的代码,我正在尝试获取当前的经度和纬度, 所以我的位置将显示在地图视图上。 但是,位置管理器委托永远不会被调用,所以我总是得到经度= 0.0000和纬度= 0.0000 .....

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

@interface MapViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property(retain,nonatomic)CLLocationManager *locationManager;
@property(assign,nonatomic)float longitude;
@property(assign,nonatomic)float latitude;

@end

这是实现文件:

  #import "MapViewController.h"
    #define METERS_PER_MILE 1609.344

    @interface MapViewController ()

    @end

    @implementation MapViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }




      - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.locationManager = [[CLLocationManager alloc] init];

        [self getCurrentLocation];

        CLLocationCoordinate2D zoomLocation;
        zoomLocation.latitude = self.latitude;
        zoomLocation.longitude = self.longitude;

        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

        [self.mapView setRegion:viewRegion animated:YES];

        NSLog(@"longitu

de %f", self.longitude);
    NSLog(@"latitude %f", self.latitude);


}

    -(void)getCurrentLocation
    {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager startUpdatingLocation];
    }


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

    - (void)viewDidUnload {
        [self setMapView:nil];
        [super viewDidUnload];
    }




    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
        UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
        CLLocation *currentLocation = [locations lastObject];
        if (currentLocation != nil) {
            self.longitude = currentLocation.coordinate.longitude;
            self.latitude = currentLocation.coordinate.latitude;
            NSLog(@"cal longitude %f", self.longitude);
            NSLog(@"cal latitude %f", self.latitude);
        }
    }
    @end
请允许任何人给我一些建议。欢呼声

1 个答案:

答案 0 :(得分:0)

可能是您在模拟器上运行代码而SDK低于6.0。在这种情况下,纬度和经度只有0.0000。并确保授予应用程序访问设备位置的权限。

如果您只想获取当前位置,请执行以下操作:

-(void)getCurrentLocation
{
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    CLLocation currentLocation = self.locationManager.location;
    [self.locationManager stopUpdatingLocation];
}

BTW,在初始化任何对象时永远不要使用self。因为这样做会创建2个对象。

第一个对象是由此行self.locationManager = [[CLLocationManager alloc] init];的右侧创建的。当setter方法被同一行的左侧调用时,会创建第二个对象,以将对象分配给locationManager变量。