您的位置:首页 > 移动开发 > IOS开发

IOS定位,获取经纬度,IOS8环境

2015-06-02 11:21 351 查看
//
//  FLShopsCtl.m
//  eher365
//
//  Created by fulin on 15/6/1.
//  Copyright (c) 2015年 fulin. All rights reserved.
//

/*****************************

*****************************/

#import <CoreLocation/CoreLocation.h>
#import "FLShopsCtl.h"
#import "FLShopsTableViewCell.h"
#import "FLSearchShopsCtl.h"
#import "FLShopsDetailCtl.h"

@interface FLShopsCtl () <CLLocationManagerDelegate>

- (IBAction)clickWithSearchView;

@property(nonatomic, strong)    NSString* currentLatitude;
@property(nonatomic, strong)    NSString* currentLongitude;
@property(nonatomic, strong)    CLLocationManager *locationManager;

@end

@implementation FLShopsCtl
static NSString * const reuseIdentifier = @"shops_id";

- (void)viewDidLoad
{
//定位
[self setupMapGetAddress];

}

//------------------------------------------------------------------------------------------------------------
//定位

- (void)setupMapGetAddress
{
CLLocationManager *locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locManager.distanceFilter = 5.0;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 8.0)
{
[locManager requestWhenInUseAuthorization];
[locManager requestAlwaysAuthorization];
}
[locManager startUpdatingLocation];
self.locationManager = locManager;
}

#pragma mark - CLLocationManagerDelegate 定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];

CLLocation *newLocation = [locations objectAtIndex:0];
NSString *strLat = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.latitude];
NSString *strLng = [NSString stringWithFormat:@"%.4f",newLocation.coordinate.longitude];
NSLog(@"Lat: %@  Lng: %@", strLat, strLng);
}

#pragma mark - CLLocationManagerDelegate 定位失败
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[self.locationManager stopUpdatingLocation];

//定位失败, 找到原因,并且处理
}

//------------------------------------------------------------------------------------------------------------

@end


补充:

iOS8中定位服务的变化(CLLocationManager协议方法不响应,无法回掉GPS方法,不出现获取权限提示)

以下内容转载:

最近在写一个LBS的项目的时候,因为考虑到适配iOS8,就将项目迁移到Xcode6.0.1上,出现了不能正常获取定位服务权限的问题。

self.manger = [[CLLocationManager alloc]init];
self.manger.distanceFilter = kCLDistanceFilterNone; // meters
self.manger.delegate = self;
[self.manger requestAlwaysAuthorization];
self.manger.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[self.manger startUpdatingLocation];


以上是iOS8之前的调用方法,当用户开启App的时候,会主动询问是否允许开启定位服务权限,现在在iOS8中,无论是模拟器还是真机,都不进行提示,经过一番查阅官方文档-->LocationAwarenessPGIntroduction 发现,Apple在iOS8中加强了隐私访问权限的控制,必须调用新的方法来获取用户的允许

[self.manager requestWhenInUseAuthorization];


并且在info.plist文件中增加
NSLocationWhenInUseUsageDescription BOOL YES

NSLocationAlwaysUsageDescription string “提示描述”

两个字段,在iOS8中才能进行正确的获取服务权限

在使用高德和百度地图的时候,出现了编译错误,大致原因是因为Xcode6中默认为使用64编译造成了,如果解决,后期博文会进行更新

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: