gpt4 book ai didi

ios - 为什么纬度和经度在 ios 9 中显示为零?

转载 作者:可可西里 更新时间:2023-11-01 17:14:24 25 4
gpt4 key购买 nike

我必须找到当前位置,我正在使用此代码

 -(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;

- (void)getCurrentLocation{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
NSLog(@"Latitude = %@", latitude);
NSLog(@"Longitude = %@", longitude);
}

但纬度和经度即将归零?我在 viewDidLoad

中调用了这些方法
[self getCurrentLocation];
[self getLocation];

为什么会这样,请帮帮我。谢谢

最佳答案

希望,以下信息可以帮助到您:

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

@interface yourController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}

@end

并添加:

    locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];

[locationManager startUpdatingLocation];

回调函数

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}

您还必须为

添加一个字符串
[`NSLocationAlwaysUsageDescription`]

关于ios - 为什么纬度和经度在 ios 9 中显示为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37538180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com