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);
计算的是大概的距离,可能没有那么精准。输入结果为:
1
|
distance = 1066449.749194 |