在iOS App中實(shí)現(xiàn)地理位置定位的基本方法解析
iOS系統(tǒng)自帶的定位服務(wù)可以實(shí)現(xiàn)很多需求。比如:獲取當(dāng)前經(jīng)緯度,獲取當(dāng)前位置信息等等。
其定位有3種方式:
1,GPS,最精確的定位方式
2,蜂窩基站三角定位,這種定位在信號(hào)基站比較秘籍的城市比較準(zhǔn)確。
3,Wifi,這種方式貌似是通過網(wǎng)絡(luò)運(yùn)營(yíng)商的數(shù)據(jù)庫(kù)得到的數(shù)據(jù),在3種定位種最不精確
首先你要在你的Xcode中添加兩個(gè)連接庫(kù),MapKit和CoreLocation,如圖

core location提供了定位功能,能定位裝置的當(dāng)前坐標(biāo),同時(shí)能得到裝置移動(dòng)信息,最重要的類是CLLocationManager,定位管理。
iOS8開始,Core Location framework的變化主要有以下幾點(diǎn):
1. 在定位狀態(tài)中引入Always 和WhenInUse的概念。
2. 加入Visit monitoring的特性, 這類特性特別適合旅行類別的應(yīng)用,當(dāng)用戶到達(dá)某個(gè)指定的區(qū)域內(nèi),monitor開始作用。
3.加入室內(nèi)定位技術(shù),增加CLFloor, 在室內(nèi)可以得到樓層信息。
獲取當(dāng)前經(jīng)緯度
首先導(dǎo)入#import <CoreLocation/CoreLocation.h>,定義CLLocationManager的實(shí)例,實(shí)現(xiàn)CLLocationManagerDelegate。
@interface ViewController ()<CLLocationManagerDelegate>
{
CLLocationManager *_locationManager;
}
@end
開始定位的方法:
- (void)startLocating
{
if([CLLocationManager locationServicesEnabled])
{
_locationManager = [[CLLocationManager alloc] init];
//設(shè)置定位的精度
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
_locationManager.distanceFilter = 100.0f;
_locationManager.delegate = self;
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 8.0)
{
[_locationManager requestAlwaysAuthorization];
[_locationManager requestWhenInUseAuthorization];
}
//開始實(shí)時(shí)定位
[_locationManager startUpdatingLocation];
}
}
實(shí)現(xiàn)代理方法:
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Longitude = %f", manager.location.coordinate.longitude);
NSLog(@"Latitude = %f", manager.location.coordinate.latitude);
[_locationManager stopUpdatingLocation];
}
獲取當(dāng)前位置信息
在上面的代理方法中
-(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(國(guó)家) State(城市) SubLocality(區(qū))
NSLog(@"%@", [test objectForKey:@"Country"]);
NSLog(@"%@", [test objectForKey:@"State"]);
NSLog(@"%@", [test objectForKey:@"SubLocality"]);
NSLog(@"%@", [test objectForKey:@"Street"]);
}
}];
}
這樣就很簡(jiǎn)單獲取了當(dāng)前位置的詳細(xì)信息。
獲取某一個(gè)地點(diǎn)的經(jīng)緯度
- (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);
}
}];
}
計(jì)算兩個(gè)地點(diǎn)之間的距離
- (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方法獲取某一個(gè)地點(diǎn)的經(jīng)緯度。比如我們獲取北京和上海的經(jīng)緯度分別為:北京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);
計(jì)算的是大概的距離,可能沒有那么精準(zhǔn)。輸入結(jié)果為:
distance = 1066449.749194
相關(guān)文章
iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器使用進(jìn)階
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器的使用進(jìn)階,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
關(guān)于iOS中的各種顏色設(shè)置總結(jié)大全(推薦)
這篇文章主要給大家介紹了關(guān)于iOS中顏色設(shè)置的相關(guān)資料,其中包括導(dǎo)航欄、狀態(tài)欄、Tabbar、Button、TextField、AttributedString和通用部分的顏色設(shè)置方法示例,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2017-09-09
IOS開發(fā)之判斷兩個(gè)數(shù)組中數(shù)據(jù)是否相同實(shí)例詳解
這篇文章主要介紹了IOS開發(fā)之判斷兩個(gè)數(shù)組中數(shù)據(jù)是否相同實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
兩種iOS調(diào)用系統(tǒng)發(fā)短信的方法
iOS調(diào)用系統(tǒng)的發(fā)短信功能可以分為兩種:1,程序外調(diào)用系統(tǒng)發(fā)短信。2,程序內(nèi)調(diào)用系統(tǒng)發(fā)短信。第二種的好處是用戶發(fā)短信之后還可以回到app。這對(duì)app來說非常重要。2016-07-07
iOS13適配三指撤銷和文案限長(zhǎng)實(shí)例詳解
這篇文章主要為大家介紹了iOS13適配三指撤銷和文案限長(zhǎng)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

