无法通过Corelocation查找位置

时间:2015-10-09 07:45:10

标签: ios objective-c core-location

我使用corelocation来检测在URL中使用的纬度和经度,但是日志在两者中都打印了0.00000

这是我的代码

mapViewcontroller.m

#import <CoreLocation/CoreLocation.h>

@interface mapsViewController () <CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *mapsweb;
@property(weak,nonatomic) CLLocationManager *locationmanager;
@end

@implementation mapsViewController

-(NSString *)getlat{
    return [NSString stringWithFormat:@"%f",_locationmanager.location.coordinate.latitude];
}
-(NSString *)getlon{
    return [NSString stringWithFormat:@"%f",_locationmanager.location.coordinate.longitude];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _locationmanager = [[CLLocationManager alloc]init];
    _locationmanager.distanceFilter = kCLDistanceFilterNone;
    _locationmanager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [_locationmanager startUpdatingLocation];

    NSString *t1 = @"http://mymaps.com/clat/";
    NSString *la = [self getlat];
    NSString *t2 = @"/clng/";
    NSString *lo = [self getlon];

    NSLog(@"latitude is %@",[self getlat]);
    NSLog(@"Longitude is %@",[self getlon]);

2 个答案:

答案 0 :(得分:1)

您没有使用requestWhenInUseAuthorization或requestAlwaysAuthorization

还要检查下面的代表 -

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}

答案 1 :(得分:1)

您是否在Info.plist中添加了NSLocationWhenInUseUsageDescription?如果没有,则添加此属性并使其类型为String,value = requestWhenInUseAuthorization