iOS系統(tǒng)自帶的定位服務(wù)可以實現(xiàn)很多需求咽袜。比如:獲取當(dāng)前經(jīng)緯度枕稀,獲取當(dāng)前位置信息等等。
獲取當(dāng)前經(jīng)緯度
首先導(dǎo)入#import 凹联,定義CLLocationManager的實例哆档,實現(xiàn)CLLocationManagerDelegate。
@interface ViewController ()
{
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];
}
//開始實時定位
[_locationManager startUpdatingLocation];
}
}
實現(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(國家) ?State(城市) ?SubLocality(區(qū))
NSLog(@"%@", [test objectForKey:@"Country"]);
NSLog(@"%@", [test objectForKey:@"State"]);
NSLog(@"%@", [test objectForKey:@"SubLocality"]);
NSLog(@"%@", [test objectForKey:@"Street"]);
}
}];
}
這樣就很簡單獲取了當(dāng)前位置的詳細(xì)信息。
獲取某一個地點的經(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);
}
}];
}
計算兩個地點之間的距離
- (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)緯度杠巡。(有問題可以關(guān)注微信iOS開發(fā):iOSDevTip)比如我們獲取北京和上海的經(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);
計算的是大概的距離,可能沒有那么精準(zhǔn)厘线。輸入結(jié)果為:
distance = 1066449.749194
代碼下載地址:https://github.com/worldligang/MapDistanceMaster.git