IOS培训
美国上市IOS培训机构

400-111-8989

热门课程

IOS8获取地理位置信息

  • 时间:2015-06-07
  • 发布:iOS培训
  • 来源:IOS开发教程

Info.plist设置

在plist里增加两个字段

NSLocationAlwaysUsageDescription

 

NSLocationWhenInUseUsageDescription

值都设置为NSString类型的YES

//设置

- (void)_startGPS

{

  [LocalInfosharedInstance].locationManager= [[CLLocationManageralloc]init];

  [LocalInfosharedInstance].locationManager.delegate=self;

  [LocalInfosharedInstance].locationManager.desiredAccuracy=kCLLocationAccuracyBest;

  if([[LocalInfosharedInstance].locationManagerrespondsToSelector:@selector(requestAlwaysAuthorization)])

  {

    [[LocalInfosharedInstance].locationManagerrequestWhenInUseAuthorization];

    [[LocalInfosharedInstance].locationManagerrequestAlwaysAuthorization];//永久授权

  }

  [[LocalInfosharedInstance].locationManagerstartUpdatingLocation];

  [[LocalInfosharedInstance].locationManagerperformSelector:@selector(stopUpdatingLocation)withObject:nilafterDelay:45];

 

}

//回调

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation

{

  NSTimeIntervallocationAge = -[newLocation.timestamptimeIntervalSinceNow];

  if(locationAge >5.0)return;

   

  if(newLocation.horizontalAccuracy<0)return;

   

  if([LocalInfosharedInstance].currentLocation==nil|| [LocalInfosharedInstance].currentLocation.horizontalAccuracy> newLocation.horizontalAccuracy)

  {

    [LocalInfosharedInstance].currentLocation= newLocation;

    [ConfigDataSourcegetCityInfoWith:[LocalInfosharedInstance].currentLocation];

     

    //本地更新

    [USER_DEFAULTsetObject:[NSStringstringWithFormat:@"%f",[LocalInfosharedInstance].currentLocation.coordinate.latitude]forKey:USER_LOGIN_LATITUDE];

    [USER_DEFAULTsetObject:[NSStringstringWithFormat:@"%f",[LocalInfosharedInstance].currentLocation.coordinate.longitude]forKey:USER_LOGIN_LONGITUDE];

    [USER_DEFAULTsynchronize];

     

    if(newLocation.horizontalAccuracy<= [LocalInfosharedInstance].locationManager.desiredAccuracy)

    {

      [[LocalInfosharedInstance].locationManagerstopUpdatingLocation];

    }

  }

 

}

上一篇:Objective-C内存管理教程
下一篇:IOS开发相关设置积累

iOS、Android 开发单位换算及 UI 切图要求

【手游开发篇】IOS系统下进程模块的遍历

iOS之基于FreeStreamer的简单音乐播放器

iOS性能调优方法,iOS性能调优教程

选择城市和中心
贵州省

广西省

海南省