//
//  ViewController.m
//  LBS
//
//  Created by tonnyhuang on 15/8/28.
//  Copyright (c) 2015年 tonnyhuang. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

//首先,我们需要在工程中导入CoreLocation系统框架。然后在我们的控制器中引入头文件。
//然后,声明一个CLLocationManager对象作为成员变量,用于定位获取经纬度坐标,并遵守协议CLLocationManager的协议。
@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}

@end

@implementation ViewController

//实现其中的代理方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    //获取经度
    NSLog(@"经度 == %lf", newLocation.coordinate.longitude);
    //获取纬度
    NSLog(@"纬度 == %lf", newLocation.coordinate.latitude);
    //获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编码出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        NSLog(@"%@", placemark.name);
        //获取城市
        NSString *city = placemark.locality;
       
        //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市
       
        if (!city) {
            city = placemark.administrativeArea;
        } else if (error == nil && [placemarks count] == 0){
            NSLog(@"no result were returned");
        } else if (error != nil) {
            NSLog(@"error = %@", error);
        }
        NSLog(@"city = %@", city);
    }];
    //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
}

//最后在viewDidLoad中初始化定位管理器。
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeLocationService];
}

- (void)initializeLocationService {
    //初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    //设置代理
    _locationManager.delegate = self;
    //设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //开始定位
    [_locationManager requestAlwaysAuthorization];
    //取得定位权限,有两个方法,取决于你的定位使用情况
    [_locationManager startUpdatingLocation];
}
//如果需要正常定位,相对iOS7而言,iOS8需要额外处理两个地方。//1. 工程的plist文件里面添加两个字段:NSLocationAlwaysUsageDescription,

//NSLocationWhenInUseUsageDescription,type类型均为string,值可以根据你的需要填写(也可以不填),填写的内容会展示在APP提示用户是否允许定位的alert内容里面,具体效果可以自行测试,这里就不额外截图。
 
这儿的位置不要错误  在上边的info.plist中添加字段
 
 
//2. 调用定位方法之前需要额外调用一个函数,直接在上面iOS7初始化定位服务的方法里面修改即可,具体如下:

// 开始定位
// 取得定位权限,有两个方法,取决于你的定位使用情况
// 一个是requestAlwaysAuthorization,一个是requestWhenInUseAuthorization
//[_locationManager requestAlwaysAuthorization];//这句话ios8以上版本使用。
//[_locationManager startUpdatingLocation];

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

使用系统的CoreLocation定位的更多相关文章

  1. iOS - CoreLocation 定位

    前言 NS_CLASS_AVAILABLE(10_6, 2_0) @interface CLLocationManager : NSObject 1.CoreLocation 定位 配置 1.在 iO ...

  2. iOS开发拓展篇—CoreLocation定位服务

    iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...

  3. iOS-----使用CoreLocation定位

    使用CoreLocation定位 CoreLocation框架 (CoreLocation.framework)可用于定位设备当前经纬度, 通过该框架, 应用程序可通过附近的蜂窝基站\WIFI信号 或 ...

  4. iOS8中使用CoreLocation定位[转]

    本文转自:http://blog.devzeng.com/blog/ios8-corelocation-framework.html iOS8以前使用CoreLocation定位 1.首先定义一个全局 ...

  5. CoreLocation定位技术

    CoreLocation框架可用于定位设备当前经纬度,通过该框架,应用程序可通过附近的蜂窝基站,WIFI信号或者GPS等信息计算用户位置.      iOS定位支持的3种模式.      (1)GPS ...

  6. CoreLocation 定位

    前言: 本章会使用OC和Swift分别进行实现,需要了解Swift的小伙伴可以翻一下之前的博文 LBS和SoloMo(索罗门) LBS:基于位置的服务,根据定位展示周边美食.景点等信息(全称:Loca ...

  7. CoreLocation定位

    nCoreLocation   n简介 n在移动互联网时代,移动app能解决用户的很多生活琐事,比如 p导航:去任意陌生的地方 p周边:找餐馆.找酒店.找银行.找电影院 p n在上述应用中,都用到了地 ...

  8. iOS iOS9.0 的CoreLocation定位

    一.简介 iOS9.0如果当前处于前台授权状态,默认是不可以后台获取用户位置. 如果在前台授权下,让其能获取到后台定位,该怎么办 可以设置以下属性为YES,就可以继续获取后台位置,但是会出现蓝条 使用 ...

  9. Android 系统api实现定位及使用百度提供的api来实现定位

    目前在国内使用定位的方法主要是 1. Android系统提供的 LocationManager locationManager = (LocationManager) getSystemService ...

随机推荐

  1. X86汇编概要

    来自:https://www.cnblogs.com/jiftle/p/8453106.html 本文翻译自:http://www.cs.virginia.edu/~evans/cs216/guide ...

  2. linux文件格式转换:<U+FEFF> character showing up in files. How to remove them?

    You can easily remove them using vim, here are the steps: 1) In your terminal, open the file using v ...

  3. ubuntu搭建ftp服务器

    (1).首先用命令检查是否安装了vsftpd vsftpd -version  如果未安装用一下命令安装 sudo apt-get install vsftpd 安装完成后,再次输入vsftpd -v ...

  4. sharepoint 调查问卷权限设置

    参考网址:http://www.cnblogs.com/mybi/archive/2011/04/18/2019935.html 按文章设置后发现访问时提示没有权限. 于是把新权限(问卷回复)的权限组 ...

  5. spring JPA分页排序条件查询

    @RequestMapping("/listByPage") public Page<Production> listByPage(int page, int size ...

  6. 安装配置Windows Live Writer做为博客客户端

    前言: 国内好些空间.博客是支持Windows Live Writer客户端的,也就是说使用Windows Live Writer不用登陆博客网站,就可以向不同的博客网站发布博客了. Windows ...

  7. 【校招面试 之 C/C++】第30题 C++ 11新特性(一)之auto关键字

    1.自动类型推断 auto自动类型推断,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推断,可以大大简化我们的编程工作.下面是一些使用auto的例子. #include <ve ...

  8. [leetcode]299. Bulls and Cows公牛和母牛

    You are playing the following Bulls and Cows game with your friend: You write down a number and ask ...

  9. 表单数据转换成json格式数据

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  10. SqlDataHelper

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...