国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

解析iOS App中實(shí)現(xiàn)地理位置定位的基本方法

2020-02-19 15:47:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

做過網(wǎng)絡(luò)側(cè)定位的學(xué)生可能知道,無(wú)論哪張網(wǎng)絡(luò)服務(wù)API地圖,最終定位誤差都非常大,無(wú)法與移動(dòng)應(yīng)用定位進(jìn)行比較,因?yàn)橐苿?dòng)應(yīng)用定位的原理是基于GPS或基站定位,本文是武林技術(shù)頻道小編介紹的解析iOS App中實(shí)現(xiàn)地理位置定位的基本方法,希望對(duì)你學(xué)習(xí)有幫助。
其定位有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,如圖

201651091651873.jpg (856×368)

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 App中實(shí)現(xiàn)地理位置定位的基本方法的相關(guān)介紹,隨著IT行業(yè)的高速發(fā)展,這方面的人才的需求都是很大的,武林技術(shù)頻道小編會(huì)為大家搜集更多的知識(shí)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 云林县| 邵东县| 全椒县| 资中县| 竹溪县| 永和县| 武鸣县| 耿马| 新沂市| 威信县| 祁连县| 双江| 巴东县| 桂平市| 定日县| 汕尾市| 田东县| 洞头县| 怀安县| 武威市| 内乡县| 前郭尔| 昌乐县| 方山县| 宾阳县| 渭源县| 广德县| 温泉县| 盐池县| 景洪市| 碌曲县| 晋城| 宜城市| 纳雍县| 凤翔县| 孟津县| 林西县| 措勤县| 大城县| 广州市| 江城|