Ios 正在尝试获取当前位置,但从未调用LocationManager委托
这是我的代码,我想知道我现在的经度和纬度, 所以我的位置将显示在地图视图上。 但是,从未调用location manager代理,因此我总是得到经度=0.0000和纬度=0.0000Ios 正在尝试获取当前位置,但从未调用LocationManager委托,ios,delegates,location,mapkit,cllocationmanager,Ios,Delegates,Location,Mapkit,Cllocationmanager,这是我的代码,我想知道我现在的经度和纬度, 所以我的位置将显示在地图视图上。 但是,从未调用location manager代理,因此我总是得到经度=0.0000和纬度=0.0000 #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface MapViewController : UIViewController <
#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
谁能给我一些建议吗。值得高兴的是,您可能正在模拟器上运行代码,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];
}
顺便说一句,初始化任何对象时,切勿使用self
。因为这样做会创建两个对象
第一个对象是在此行的右侧创建的
self.locationManager=[[CLLocationManager alloc]init]代码>。第二个对象是在同一行左侧调用setter方法时创建的,用于将对象分配给locationManager
变量。调用委托方法可能需要一些时间。。我怀疑你会在调用你的视图之前得到这些值。它不会被调用吗?如果委托方法locationManager:didUpdateLocations
根本没有被调用,那么请确保您正在iOS 6上运行该应用程序,并且该应用程序被授权使用位置服务。
-(void)getCurrentLocation
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
CLLocation currentLocation = self.locationManager.location;
[self.locationManager stopUpdatingLocation];
}