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

首頁 > 系統 > iOS > 正文

在iOS App中實現地理位置定位的基本方法解析

2020-07-26 03:20:55
字體:
來源:轉載
供稿:網友

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

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

201651091651873.jpg (856×368)

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 女性| 定襄县| 县级市| 滦南县| 通城县| 西丰县| 邹城市| 海门市| 庆城县| 杨浦区| 茶陵县| 平利县| 鄱阳县| 星子县| 茂名市| 平邑县| 高要市| 区。| 新巴尔虎右旗| 新乡县| 兴海县| 柯坪县| 大同市| 芦山县| 克什克腾旗| 怀远县| 巴青县| 宜兰市| 含山县| 五华县| 会同县| 虹口区| 阿拉尔市| 固安县| 尼木县| 越西县| 繁昌县| 思南县| 措美县| 临潭县| 内乡县|