代码之家  ›  专栏  ›  技术社区  ›  Shriram Kadam

如何在目标c中以19.0176°N、72.8562°E的格式显示纬度和经度

  •  0
  • Shriram Kadam  · 技术社区  · 9 年前

    我使用以下代码获取当前位置,我添加了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,经度:72°51 22.1918,所以我需要在目标c中以19.0176°N,72.8562°E的格式显示坐标。如何获取准确的纬度和经度。如果我犯了错误,或者我需要添加任何其他方法、函数、框架或示例代码。

    2 回复  |  直到 9 年前
        1
  •  0
  •   tuledev    9 年前

    您已经拥有这些值,请参见注释 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;
    
    
        }
    
        2
  •  0
  •   serdaryillar    9 年前
      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";
      }
    }