Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS8/ios9中使用CoreLocation?_Ios - Fatal编程技术网

如何在iOS8/ios9中使用CoreLocation?

如何在iOS8/ios9中使用CoreLocation?,ios,Ios,我已经编写了一个使用CoreLocation的项目。在这个项目中,我设计了一个按钮来获取位置。然而,当我点击时,它没有反应。有人说它在iOS 8中发生了变化。我照他说的做。但它不起作用。这是我的git链接:在CurrentLocationViewController中执行类似操作,getLocation方法更改如下:- locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [

我已经编写了一个使用CoreLocation的项目。在这个项目中,我设计了一个按钮来获取位置。然而,当我点击时,它没有反应。有人说它在iOS 8中发生了变化。我照他说的做。但它不起作用。这是我的git链接:

在CurrentLocationViewController中执行类似操作,getLocation方法更改如下:-

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    [locationManager requestAlwaysAuthorization];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
   _locationManager.allowsBackgroundLocationUpdates = YES;
}
[locationManager setDistanceFilter:10.0f];
[locationManager startUpdatingLocation];
[locationManager startMonitoringSignificantLocationChanges];
首先在.Plist文件中设置两行

NSLocationAlwaysUsageDescription:允许位置

NSLocationWhenUsageDescription:允许位置

//AppDelegate.h
#进口
@财产(非原子,保留)CLLocationManager*locationManager;
#导入“AppDelegate.m”
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
//应用程序启动后自定义的覆盖点。
[自获取当前位置];
}
#pragma标记-CLLocatin委托和位置方法
-(void)getCurrentLocation{
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
//计算500米上的摆动
/*CLLocationDistance KM=0.5 1000.0;//当距离比当前距离更改40公里时,将通知用户
locationManager.distanceFilter=公里*/
#ifdef\uuuIphone\u8\u0
如果(是8或更高版本)
{
[locationManager请求始终授权];
}
#恩迪夫
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
//NSLog(@“didUpdateLocation:%@”,newLocation);
CLLocation*currentLocation=newLocation;
如果(当前位置!=nil){
[locationManager停止更新位置];
[本国];
}
}
-(无效)locationManager:(CLLocationManager*)管理员错误:(N错误*)错误{
开关([错误代码])
{
案例kCLErrorNetwork://general,网络相关错误
{
}
打破
案件被驳回:{
}
打破
违约:
{
}
打破
}
}
-(无效)当前国家/地区{
CLGeocoder*geoCoder=[[CLGeocoder alloc]init];
[地理编码器反转地理位置:locationManager.location
completionHandler:^(NSArray*放置标记,NSError*错误){
如果(错误==nil&&[placemarks count]>0)
{
NSLog(@“当前国家:%@,[[placemarks objectAtIndex:0]国家]);
NSLog(@“当前国家/地区代码:%@,[[placemarks objectAtIndex:0]等国家/地区代码]);
NSLog(@“CountryCode=%@”,GetContryCode);
SetContryCode
setBoolForCountryCode(是);
NSLog(@“CountryCode=%@”,GetContryCode);
}
}];
}

我在项目中发现了问题。我认为名为_locationManager的实例变量尚未在initWithCoder中初始化。我的initWithCoder有什么问题吗?我将_locationManager的初始化放在getLocation中。那就行了。伟大的如果对你有帮助的话,请把答案投上去。继续编码,朋友@戴王炯我在我的项目中发现了这个问题。我认为名为_locationManager的实例变量尚未在initWithCoder中初始化。我的initWithCoder有什么问题吗?我将_locationManager的初始化放在getLocation中。然后它就起作用了。
To Get Current Location, Follow step..
//  AppDelegate.h
#import <CoreLocation/CoreLocation.h>
@property (nonatomic,retain) CLLocationManager *locationManager;

#import "AppDelegate.m"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self getCurrentLocation];
}

#pragma mark - CLLocatin delegate && Location Methdos

-(void)getCurrentLocation {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // To calculate loaction on 500 meters

    /* CLLocationDistance kilometers = 0.5 1000.0; //user will be notified when distance is changed by 40km from current distance
    locationManager.distanceFilter = kilometers; */
#ifdef __IPHONE_8_0
    if (IS_OS_8_OR_LATER)
    {
        [locationManager requestAlwaysAuthorization];
    }
#endif
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
        [locationManager stopUpdatingLocation];
        [self getCurrentCountry];
    }
}

- (void)locationManager:(CLLocationManager*)aManager didFailWithError:(NSError *)anError {
    switch([anError code])
    {
        case kCLErrorNetwork: // general, network-related error
        {
        }
            break;
        case kCLErrorDenied:{
        }
            break;
        default:
        {
        }
            break;
    }
}

-(void)getCurrentCountry {
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:locationManager.location
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                        if (error == nil && [placemarks count] > 0)
                        {
                            NSLog(@"Current country: %@", [[placemarks objectAtIndex:0] country]);
                            NSLog(@"Current country code: %@", [[placemarks objectAtIndex:0] ISOcountryCode]);

                            NSLog(@"CountryCode=%@",GetContryCode);

                            SetContryCode
                            setBoolForCountryCode(YES);
                            NSLog(@"CountryCode=%@",GetContryCode);
                        }
                   }];
}