1.引入CoreLocation.framework,#import <CoreLocation/CoreLocation.h>,添加委托CLLocationManagerDelegate

2.

{
    float Currentlat,Currentlon;
    CLLocationManager *locationManager;
    UILabel *cityLabel;
}

3.viewDidLoad里添加

if ([CLLocationManager locationServicesEnabled] == NO) {
        NSLog(@"没有GPS服务");   
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"警告" message:@"没有GPS服务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alertView show];   
    }else{
        CLLocationManager *locmanager = [[CLLocationManager alloc]init];//先定义一个cllocationmanager的实例
        locationManager=locmanager;
        [locationManager requestAlwaysAuthorization];//相对应info.plist中的NSLocationAlwaysUsageDescription键
        [locmanager setDelegate:self]; //设置代理为本身
        [locmanager setDesiredAccuracy:kCLLocationAccuracyBest];//设置精确度为最准确
        locmanager.distanceFilter = 1000.0f;
        [locationManager startUpdatingLocation];
    }

4.
//ios6.0以上
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *newLocation = [locations lastObject];
    CLLocationCoordinate2D coor = newLocation.coordinate;
    Currentlat =  coor.latitude;
    Currentlon = coor.longitude;
    NSLog(@"当前维度ios7 %f 当前经度 %f",Currentlat,Currentlon);
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
        for (CLPlacemark * placemark in placemarks) {
            NSDictionary *test = [placemark addressDictionary];
            //  Country(国家)  State(城市)  SubLocality(区)
            cityLabel.text=[test objectForKey:@"State"];
        }
    }];
    [manager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"定位失败");
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"定位失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

5.

ios8必须注意:

iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:

(1)始终允许访问位置信息

- (void)requestAlwaysAuthorization;

(2)使用应用程序期间允许访问位置数据

- (void)requestWhenInUseAuthorization;

在运行开始更新前加下面这句

[locationManager requestAlwaysAuthorization];//相对应info.plist中的NSLocationAlwaysUsageDescription键

2、在Info.plist文件中添加如下配置:

(1)NSLocationAlwaysUsageDescription

(2)NSLocationWhenInUseUsageDescription

这两个键的值就是授权alert的描述,示例配置如下[勾选Show Raw Keys/Values后进行添加]:

额外附赠:

五、根据两点坐标计算两点之间的距离,此方法为苹果自带方法,亲测速度比高德API速度快很多,但是数据与高德API得到的不一样,准确度本人未能证实

//第一个坐标

CLLocation *current=[[CLLocation alloc] initWithLatitude:32.178722 longitude:119.508619];

//第二个坐标

CLLocation *before=[[CLLocation alloc] initWithLatitude:32.206340 longitude:119.425600];

// 计算距离

CLLocationDistance meters=[current distanceFromLocation:before];

 
 
 
 
 

iOS 获得当前经纬度和城市的更多相关文章

  1. php根据经纬度获取城市名

    /*php根据经纬度获取城市名*/ function get_my_addr_infos(){ $ch = curl_init(); $timeout = 5; $lat = $list['info' ...

  2. python地址解析经纬度,城市

    1.地址列表 1.txt 上海市普陀区梅川路299-301号 浙江省杭州市拱墅区丰登路305-311号1层 江苏省南京市鼓楼区碧树园86号101室 浙江省宁波市江北区范江岸路38弄6号-10号1层商铺 ...

  3. php 获取客户端IP地址经纬度所在城市

    1. [代码]获取客户端IP地址经纬度所在城市 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php   $getIp=$_SERVER["REMOTE_ADDR ...

  4. 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...

  5. AJ学IOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

    AJ分享,必须精品 一:效果 输入经纬度,可以得到相应的地名 二:思路 跟地里编码差不多 1.获取用户输入的经纬度 2.根据用户输入的经纬度创建CLLocation对象 3.根据CLLocation对 ...

  6. iOS之获取经纬度并通过反向地理编码获取详细地址

    _locationManager = [[CLLocationManager alloc] init]; //期望的经度 _locationManager.desiredAccuracy = kCLL ...

  7. iOS 获取当前经纬度

    一般说来LBS功能一般分为两块:一块是地理定位,就是获取当前精度.纬度和地理位置的功能,这一部分功能主要用到CoreLocation.Frameworks.一部分就是显示地图信息.丰富地图内容等,这一 ...

  8. 【Python】使用geocoder找出本机IP所在经纬度和城市

    代码: import geocoder g = geocoder.ip('me') print(g.latlng) # 经纬度 print(g.city) # 所在城市 输出: C:\Users\ho ...

  9. 微信小程序获取经纬度所在城市

    小程序的wx.getLocation()获得是经纬度并不包含地名,所以要通过经纬度用相应的地图转换出地名(本文使用的是百度地图) // 获取坐标 onLoad: function (options)  ...

随机推荐

  1. JavaScript 总结几个提高性能知识点

    前段时间花时间看了大半的<High Performance JavaScript>这本书啊,然后就开始忙项目了,庆幸最忙的一周已经熬过去了.由于空不出时间,这个月写的学习笔记也不多,忙完最 ...

  2. 初用protobuf-csharp-port

    下面这个用法是参照protobuf-csharp-port的官方wiki,参见: https://code.google.com/p/protobuf-csharp-port/wiki/Getting ...

  3. 可扩展性 Scalability

    水平扩展和垂直扩展: Horizontal and vertical scaling Methods of adding more resources for a particular applica ...

  4. P和NP问题

    1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html   NP----非定常多项式(英语:non-determin ...

  5. Linux企业集群用商用硬件和免费软件构建高可用集群PDF

    Linux企业集群:用商用硬件和免费软件构建高可用集群 目录: 译者序致谢前言绪论第一部分 集群资源 第1章 启动服务 第2章 处理数据包 第3章 编译内容 第二部分 高可用性 第4章 使用rsync ...

  6. 1018MYSQL数据迁移到SQLSERVER

    -- 第一步利用MYSQL将数据结果的脚本迁移出来-- 第二步利用POWERDESGINER的反向功能,将脚本生成为物理模型 FILE-REVERSE DATEBASE -- 第三步将物理模型生成SQ ...

  7. javascript: return return false

    一:代码(王工)var flag=true; $(function(){ $("#ff").submit(function(){ // 表单submit事件 registerUse ...

  8. 第七章 java基础类库

    1. 日期时间: 用Calendar类. 2. 分隔符:空格.tab.回车. 3. Scanner:读取键盘输入.读取文件. 4. 系统类: System  Runtime. 5. 所有的java类都 ...

  9. Ueditor 上传图片 如何设置只显示 本地上传

    我这个是自问自答,其实很简单.只要按照以下方式修改就可以了. 找到image.html 将以下代码 <div id="tabHeads" class="tabhea ...

  10. Extjs 使用图标字体来美化按钮)

    1. 使用Font Awesome,下载地址http://www.bootcss.com/p/font-awesome/#icons-new 2. 把font和css目录放到 Ext的app目录下面 ...