iOS地圖定位使用幾個小功能

來源:互聯網
上載者:User

iOS地圖定位使用幾個小功能
iOS系統內建的定位服務可以實現很多需求。比如:擷取當前經緯度,擷取當前位置資訊等等。

擷取當前經緯度

首先匯入#import ,定義CLLocationManager的執行個體,實現CLLocationManagerDelegate。

@interface ViewController (){    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

代碼: https://github.com/worldligang/MapDistanceMaster.git

更多iOS技術請關注iOS開發

iOSDevTip


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.