欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年05月10日 09:20:39   作者:李剛  
這篇文章主要介紹了在iOS App中實現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計算兩點間距離等基本功能的實現(xiàn),需要的朋友可以參考下

iOS系統(tǒng)自帶的定位服務(wù)可以實現(xiàn)很多需求。比如:獲取當(dāng)前經(jīng)緯度,獲取當(dāng)前位置信息等等。
其定位有3種方式:
1,GPS,最精確的定位方式
2,蜂窩基站三角定位,這種定位在信號基站比較秘籍的城市比較準確。
3,Wifi,這種方式貌似是通過網(wǎng)絡(luò)運營商的數(shù)據(jù)庫得到的數(shù)據(jù),在3種定位種最不精確

首先你要在你的Xcode中添加兩個連接庫,MapKit和CoreLocation,如圖

201651091651873.jpg (856×368)

core location提供了定位功能,能定位裝置的當(dāng)前坐標,同時能得到裝置移動信息,最重要的類是CLLocationManager,定位管理。
iOS8開始,Core Location framework的變化主要有以下幾點:
1. 在定位狀態(tài)中引入Always 和WhenInUse的概念。
2. 加入Visit monitoring的特性, 這類特性特別適合旅行類別的應(yīng)用,當(dāng)用戶到達某個指定的區(qū)域內(nèi),monitor開始作用。
3.加入室內(nèi)定位技術(shù),增加CLFloor, 在室內(nèi)可以得到樓層信息。

獲取當(dāng)前經(jīng)緯度

首先導(dǎo)入#import <CoreLocation/CoreLocation.h>,定義CLLocationManager的實例,實現(xiàn)CLLocationManagerDelegate。

復(fù)制代碼 代碼如下:

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

@end


開始定位的方法:
復(fù)制代碼 代碼如下:

- (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];
        }
        //開始實時定位
        [_locationManager startUpdatingLocation];
    }
}

實現(xiàn)代理方法:
復(fù)制代碼 代碼如下:

-(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)前位置信息

在上面的代理方法中

復(fù)制代碼 代碼如下:

-(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(區(qū))
            NSLog(@"%@", [test objectForKey:@"Country"]);
            NSLog(@"%@", [test objectForKey:@"State"]);
            NSLog(@"%@", [test objectForKey:@"SubLocality"]);
            NSLog(@"%@", [test objectForKey:@"Street"]);
        }
    }];

}


這樣就很簡單獲取了當(dāng)前位置的詳細信息。

獲取某一個地點的經(jīng)緯度

復(fù)制代碼 代碼如下:

- (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);
        }
    }];
}

計算兩個地點之間的距離
復(fù)制代碼 代碼如下:

- (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方法獲取某一個地點的經(jīng)緯度。比如我們獲取北京和上海的經(jīng)緯度分別為:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之間的距離就是:
復(fù)制代碼 代碼如下:

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

計算的是大概的距離,可能沒有那么精準。輸入結(jié)果為:

distance = 1066449.749194

相關(guān)文章

  • iOS WKWebView中MessageHandler內(nèi)存泄漏問題的完美解決過程

    iOS WKWebView中MessageHandler內(nèi)存泄漏問題的完美解決過程

    這篇文章主要給大家介紹了關(guān)于iOS WKWebView中MessageHandler內(nèi)存泄漏問題的完美解決過程,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • iOS設(shè)置可選擇圓角方向的控件圓角

    iOS設(shè)置可選擇圓角方向的控件圓角

    在iOS開發(fā)中會遇到設(shè)置控件圓角的效果,這篇文章就給大家分享了實現(xiàn)的方法,且可以選擇圓角的方向,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • UIWebView控件中字體大小和字體樣式的修改

    UIWebView控件中字體大小和字體樣式的修改

    本文主要介紹了UIWebView控件中字體大小和字體樣式的修改,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • IOS 中彈框的實現(xiàn)方法整理

    IOS 中彈框的實現(xiàn)方法整理

    這篇文章主要介紹了IOS 中彈框的實現(xiàn)方法整理的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • iOS?xcconfig編寫示例教程

    iOS?xcconfig編寫示例教程

    這篇文章主要為大家介紹了iOS?xcconfig編寫示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • iOS之Cocoapods安裝教程(全面解析)

    iOS之Cocoapods安裝教程(全面解析)

    下面小編就為大家?guī)硪黄猧OS之Cocoapods安裝教程(全面解析)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

    iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

    這篇文章主要介紹了iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié),需要的朋友可以參考下
    2017-11-11
  • unix 編程進程控制詳細介紹

    unix 編程進程控制詳細介紹

    這篇文章主要介紹了unix 編程進程控制詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • iOS實現(xiàn)側(cè)滑欄效果

    iOS實現(xiàn)側(cè)滑欄效果

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)側(cè)滑欄效果,點擊側(cè)邊拉出相應(yīng)菜單,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 關(guān)于iOS中屬性變量setter與getter方法的理解

    關(guān)于iOS中屬性變量setter與getter方法的理解

    這篇文章主要給大家介紹了關(guān)于iOS中屬性變量setter與getter方法的相關(guān)資料,文章介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-06-06

最新評論