在iOS App中实现地理位置定位的基本方法解析_IOS

iOS系统自带的定位服务可以实现很多需求。比如:获取当前经纬度,获取当前位置信息等等。
其定位有3种方式:
1,GPS,最精确的定位方式
2,蜂窝基站三角定位,这种定位在信号基站比较秘籍的城市比较准确。
3,Wifi,这种方式貌似是通过网络运营商的数据库得到的数据,在3种定位种最不精确

首先你要在你的Xcode中添加两个连接库,MapKit和CoreLocation,如图

core location提供了定位功能,能定位装置的当前坐标,同时能得到装置移动信息,最重要的类是CLLocationManager,定位管理。
iOS8开始,Core Location framework的变化主要有以下几点:
1. 在定位状态中引入Always 和WhenInUse的概念。
2. 加入Visit monitoring的特性, 这类特性特别适合旅行类别的应用,当用户到达某个指定的区域内,monitor开始作用。
3.加入室内定位技术,增加CLFloor, 在室内可以得到楼层信息。

获取当前经纬度

首先导入#import <CoreLocation/CoreLocation.h>,定义CLLocationManager的实例,实现CLLocationManagerDelegate。

复制代码 代码如下:

@interface ViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}

@end

开始定位的方法:

复制代码 代码如下:

- (void)startLocating
{
    if([CLLocationManager locationServicesEnabled])
    {
        _locationManager = [[CLLocationManager alloc] init];
        //设置定位的精度
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        _locationManager.distanceFilter = 100.0f;
        _locationManager.delegate = self;
        if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0)
        {
            [_locationManager requestAlwaysAuthorization];
            [_locationManager requestWhenInUseAuthorization];
        }
        //开始实时定位
        [_locationManager startUpdatingLocation];
    }
}

实现代理方法:

复制代码 代码如下:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"Longitude = %f", manager.location.coordinate.longitude);
    NSLog(@"Latitude = %f", manager.location.coordinate.latitude);
    [_locationManager stopUpdatingLocation];
}

获取当前位置信息

在上面的代理方法中

复制代码 代码如下:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"Longitude = %f", manager.location.coordinate.longitude);
    NSLog(@"Latitude = %f", manager.location.coordinate.latitude);
    [_locationManager stopUpdatingLocation];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks) {
            NSDictionary *test = [placemark addressDictionary];
            //  Country(国家)  State(城市)  SubLocality(区)
            NSLog(@"%@", [test objectForKey:@"Country"]);
            NSLog(@"%@", [test objectForKey:@"State"]);
            NSLog(@"%@", [test objectForKey:@"SubLocality"]);
            NSLog(@"%@", [test objectForKey:@"Street"]);
        }
    }];

}

这样就很简单获取了当前位置的详细信息。

获取某一个地点的经纬度

复制代码 代码如下:

- (void)getLongitudeAndLatitudeWithCity:(NSString *)city
{
    //city可以为中文
    NSString *oreillyAddress = city;
    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0 && error == nil)
        {
            NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
            NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
        }
        else if ([placemarks count] == 0 && error == nil)
        {
            NSLog(@"Found no placemarks.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];
}

计算两个地点之间的距离

复制代码 代码如下:

- (double)distanceByLongitude:(double)longitude1 latitude:(double)latitude1 longitude:(double)longitude2 latitude:(double)latitude2{
    CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
    CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2];
    double distance  = [curLocation distanceFromLocation:otherLocation];//单位是m
    return distance;
}

首先我们可以用上面的getLongitudeAndLatitudeWithCity方法获取某一个地点的经纬度。比如我们获取北京和上海的经纬度分别为:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之间的距离就是:

复制代码 代码如下:

double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706];
NSLog(@"Latitude = %f", distance);

计算的是大概的距离,可能没有那么精准。输入结果为:

distance = 1066449.749194

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索ios
, 定位
地理位置
js实现地理位置定位、基本实现现代化、android定位功能实现、gps定位基本工作原理、android 定位实现,以便于您获取更多的相关知识。

时间: 2024-08-22 14:27:42

在iOS App中实现地理位置定位的基本方法解析_IOS的相关文章

iOS App中调用iPhone各种感应器的方法总结_IOS

CoreMotion框架的使用 CoreMotion框架十分强大,它不仅将加速度传感器和螺旋仪传感器进行了统一配置和管理,还为我们封装了许多算法,我们可以直接获取到设备的运动状态信息. 1.CoreMotion负责处理的数据 CoreMotion负责处理四种数据,一种是加速度数据,一种是螺旋仪数据,一种是磁感应数据,还有一种是前三种数据通过复杂运算得到的设备的运动数据.几个主要的类如下: CMAccelerommterData:设备的加速度数据 typedef struct {     doub

详解iOS App中调用AVAudioPlayer播放音频文件的用法_IOS

要给工程中添加音频,首先要导入音频的框架 AVFoundation.framework 然后新建一个类继承于UIViewController, 我这里就叫FirstVC. 首先在 AppDelegate.m中初始化根视图 复制代码 代码如下: #import "AppDelegate.h" #import "FirstVC.h" @implementation AppDelegate - (void)dealloc {     [_window release];

详解在iOS App中自定义和隐藏状态栏的方法_IOS

自定义状态栏 有时候,需要在状态栏上显示一些自定义信息,比如新浪微博的官方iOS客户端:告知用户信息处于发送队列.发送成功或者发送失败. 如上图,通过在状态栏显示自定义信息,可以给用户友好又不影响软件使用的提示. 为此,我们显得定义一个自定义状态栏类,包含一个显示信息的Label: 复制代码 代码如下: @interface CustomStatusBar : UIWindow  {      UILabel *_messageLabel;  }    - (void)showStatusMes

iOS开发中UIDatePicker控件的使用方法简介_IOS

iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式.   您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四种模式. 本篇文章简单介绍下PickerDate控件的使用 1.新建一个Singe View Application,命名为DatePickDemo,其他设置如图 2.放置控件 打开ViewController.xib,拖拽一个DatePicker控件放到界面上,再拖拽一个Button控件放到界

iOS开发中控制屏幕旋转的编写方法小结_IOS

在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: 复制代码 代码如下: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {      return (interfaceOrientation == UIInter

iOS开发中实现hook消息机制的方法探究_IOS

Method Swizzling 原理 在Objective-C中调用一个方法,其实是向一个对象发送消息,查找消息的唯一依据是selector的名字.利用Objective-C的动态特性,可以实现在运行时偷换selector对应的方法实现,达到给方法挂钩的目的. 每个类都有一个方法列表,存放着selector的名字和方法实现的映射关系.IMP有点类似函数指针,指向具体的Method实现. 我们可以利用 method_exchangeImplementations 来交换2个方法中的IMP, 我们

问题就是啊啊-iOS app中怎么实现发红包功能

问题描述 iOS app中怎么实现发红包功能 求大神指导啊啊啊啊,公司最新的想法,现在还不知道恩么弄,有时候uiszhidao的的啊真希望不要哈哈哈发 你那么大开发建设的飞机哦圣诞节发来撒可对方能卡死的叫法开始的缴费卡拉斯的减肥你看拉水泥的反馈暗示对方的大师傅 解决方案 app 评论功能实现ios开发之APP升级的实现iOS App运行在后台实现定位以及播放音频功能 解决方案二: 不就是集成一个支付功能吗?UI方面想怎么设计都可以啊

iOS APP 中的不可变模型以及一致性数据

本文讲的是iOS APP 中的不可变模型以及一致性数据, 今年早些时候,为了给用户,尤其是大部分海外的用户更快更清晰的体验,我们全面重构了我们的 iOS 应用.这次重构的其中一个目的是将我们的应用迁移到一个不可变模型的层面上.在这篇博客中,我将会讨论这样做的动机,并探索我们的新系统是如何处理模型的更新,从 API 读取新信息,以及保持数据持久性的. 为什么选择不可变模型? 因为现今许多应用都转而使用了不可变设计,'不可变模型'已经成为了一个耳熟能详的术语.不可变性意味着再初始化后模型将不可再更改

详解iOS App中UITableView的创建与内容刷新_IOS

UITableView几乎是iOS开发中用处最广的一个控件,当然也是要记相当多东西的一个控件. 创建首先创建一个新的项目,并添加一个MainViewController的Class文件 打开MainViewController.h文件 @interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic, retain) NSArray *