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. Google Zxing 二维码生成与解析

    生成二维码的开源项目可谓是琳琅满目,SwetakeQRCode.BarCode4j.Zxing...... 前端有JQuery-qrcode,同样能实现生成二维码. 选择Zxing的原因可能是对 Go ...

  2. 基于DDD的.NET开发框架 - ABP依赖注入

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  3. denounce函数:Javascript中如何应对高频触发事件

    在DOM Event的世界中,以scroll.resize.mouseover等为代表的高频触发事件显得有些与众不同.通常,DOM事件只有在明确的时间点才会被触发,比如被点击,比如XMLHttpReq ...

  4. Linux进程间通信之共享内存

    一,共享内存  内核管理一片物理内存,允许不同的进程同时映射,多个进程可以映射同一块内存,被多个进程同时映射的物理内存,即共享内存.  映射物理内存叫挂接,用完以后解除映射叫脱接. 1,共享内存的特点 ...

  5. Python 3 与 MySQL 5.6

    主要简单说下Python 3.3搭配MySQL Community Server 5.6的使用.在Python 3系列和MySQL 5.0系列里面下面的代码应该都通用.(没有验证) 准备 python ...

  6. A query was run and no Result Maps were found for the Mapped Statement 'user.insertUser!selectKey'. It's likely that neither a Result Type nor a Result Map was specified.

    使用mybatis时出现异常问题: 有如下的错误 Error querying database. Cause: org.apache.ibatis.executor.ExecutorExceptio ...

  7. 89c51中断入口地址表

    中断源   中断入口地址 外部中断0 0003H 定时器/计数器T0 000BH 外部中断1 0013H 定时器/计数器T1 001BH 串行口中断   0023H

  8. webService访问加密-Soapheader

    WebService head加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个  ...

  9. noip复习模板

    我只会这么多 tarjan:codevs 1332 void tarjan(int u) { dfn[u]=low[u]=Time++; s.push(u); for(int i=head[u];~i ...

  10. window 系统设置无线wifi

    netsh wlan set hostednetwork mode=allow ssid=huage key=243573295 如上表示设置成功 然后继续输入 netsh wlan start ho ...