如何在 Objective c 中以 19.0176° N、72.8562° E 格式显示纬度和经度
How can I display Latitude & Longitude in 19.0176° N, 72.8562° E format in Objective c
我正在使用以下代码获取当前位置,我已经添加了 Corelocation 框架
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSInteger degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lattitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
_latitudeLabel.text = lattitudeStr;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
_longitudeLabel.text = longitudeStr;
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^
(NSArray *placemarks, NSError *error)
{
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0)
{
placemark = [placemarks lastObject];
_addressLabel.text = [NSString stringWithFormat:@"%@\n%@ %@\n%@\n%@",
placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
我得到的值是,纬度:19° 1' 3.4129” 和 Longitude:72° 51' 22.1918” 所以我需要在 [=17 中以 19.0176° N,72.8562° E 格式显示坐标=].如何获得准确的纬度和经度。我在哪里做错了或者我需要添加任何其他方法或函数或框架或示例代码。
您已有这些值,请参阅评论this is what you want
if (currentLocation != nil) {
NSInteger degrees = newLocation.coordinate.latitude; //<--- this is what you want
NSString *lattitudeStr = [NSString stringWithFormat:@"%f° N",
degrees];
_latitudeLabel.text = lattitudeStr;
degrees = newLocation.coordinate.longitude; //<--- this is what you want
NSString *longitudeStr = [NSString stringWithFormat:@"%f° E",
degrees];
_longitudeLabel.text = longitudeStr;
}
NSString *lat = [self convertCoordinateFromDegreeMinuteSecond:@"19° 1’ 3.4129”" position:@"N"];
NSString *lon = [self convertCoordinateFromDegreeMinuteSecond:@"72° 51’ 22.1918”" position:@"E"];
- ( NSString * )convertCoordinateFromDegreeMinuteSecond:( NSString * )string position:( NSString * )position; {
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
NSArray *arrayLat = [string componentsSeparatedByString:@" "];
if ( arrayLat.count == 3 ) {
NSString *degree = [[arrayLat[ 0 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSString *minute = [[arrayLat[ 1 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSString *second = [[arrayLat[ 2 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
int sign = degree.doubleValue < 0 ? -1 : 1;
double coordinate = ( degree.doubleValue + ( sign * ( minute.doubleValue / 60. ) ) + ( sign * ( second.doubleValue / 3600. ) ) );
NSString *str = [NSString stringWithFormat:@"%f° %@", coordinate, position];
return str;
} else {
return @"NaN";
}
}
我正在使用以下代码获取当前位置,我已经添加了 Corelocation 框架
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSInteger degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lattitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
_latitudeLabel.text = lattitudeStr;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
_longitudeLabel.text = longitudeStr;
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^
(NSArray *placemarks, NSError *error)
{
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0)
{
placemark = [placemarks lastObject];
_addressLabel.text = [NSString stringWithFormat:@"%@\n%@ %@\n%@\n%@",
placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
我得到的值是,纬度:19° 1' 3.4129” 和 Longitude:72° 51' 22.1918” 所以我需要在 [=17 中以 19.0176° N,72.8562° E 格式显示坐标=].如何获得准确的纬度和经度。我在哪里做错了或者我需要添加任何其他方法或函数或框架或示例代码。
您已有这些值,请参阅评论this is what you want
if (currentLocation != nil) {
NSInteger degrees = newLocation.coordinate.latitude; //<--- this is what you want
NSString *lattitudeStr = [NSString stringWithFormat:@"%f° N",
degrees];
_latitudeLabel.text = lattitudeStr;
degrees = newLocation.coordinate.longitude; //<--- this is what you want
NSString *longitudeStr = [NSString stringWithFormat:@"%f° E",
degrees];
_longitudeLabel.text = longitudeStr;
}
NSString *lat = [self convertCoordinateFromDegreeMinuteSecond:@"19° 1’ 3.4129”" position:@"N"];
NSString *lon = [self convertCoordinateFromDegreeMinuteSecond:@"72° 51’ 22.1918”" position:@"E"];
- ( NSString * )convertCoordinateFromDegreeMinuteSecond:( NSString * )string position:( NSString * )position; {
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
NSArray *arrayLat = [string componentsSeparatedByString:@" "];
if ( arrayLat.count == 3 ) {
NSString *degree = [[arrayLat[ 0 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSString *minute = [[arrayLat[ 1 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
NSString *second = [[arrayLat[ 2 ] componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
int sign = degree.doubleValue < 0 ? -1 : 1;
double coordinate = ( degree.doubleValue + ( sign * ( minute.doubleValue / 60. ) ) + ( sign * ( second.doubleValue / 3600. ) ) );
NSString *str = [NSString stringWithFormat:@"%f° %@", coordinate, position];
return str;
} else {
return @"NaN";
}
}