@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; }
double distance = [self distanceByLongitude:116.405285 latitude:39.904989 longitude:121.472644 latitude:31.231706]; NSLog(@"Latitude = %f", distance);
distance = 1066449.749194
iOSDevTip