2012-11-14 2 views
1

iOS에는 현재 위치의 현재 시즌 (봄, 여름, 겨울, 가을)을 찾을 수 있습니까?iOS API를 통해 현재 시즌을 찾는 방법은 무엇입니까?

계절은 세계 곳곳에서 다릅니다. 나는 미국에서 같은시기에 다른 일이있을 때 호주에서 다른 시즌을 경험할 수도 있습니다.

날씨 API 또는 그 장소의 계절을 반환하는 항목은 무엇입니까?

+1

그것을 알아낼 꽤 간단 할 것입니다. 미국의 정상적인 계절을 사용하고 위도를 확인하십시오. 적도 아래에 있다면 겨울 - 여름, 가을 - 봄 등 ... –

+0

API에 대해 잘 모르겠지만 앱의 메소드에서 비교적 단순한 로직으로 처리 할 수 ​​있어야합니다. – HackyStack

+0

@MarcB 그렇게 간단하지 않습니다. 첫째, 변경 날짜가 일치하지 않습니다 (예 : 호주가 여름에 영국을 쳤을 때 정확히 같은 날에 겨울을 치지 않습니다). 두 번째로 적도 근처, 계절 변화가 거의없는 곳, 예 : 모리셔스에는 여름과 겨울 만 있습니다. 절대 봄/가을. – HughHughTeotl

답변

2

먼저 사용자의 현재 위치의 좌표를 얻을 :

CLLocationManager *lm = [[CLLocationManager alloc] init]; 
lm.delegate = self; 
lm.desiredAccuracy = kCLLocationAccuracyBest; 
lm.distanceFilter = kCLDistanceFilterNone; 
[lm startUpdatingLocation]; 
CLLocation *location = [lm location]; 
CLLocationCoordinate2D coord; 
coord = [location coordinate]; 

그런 다음 현재 날짜를 얻을 : 당신이 필요로하는 모든입니다

NSDate* currentDate = [NSDate date]; 

을, 나는 생각한다. 사용자가 적도의 위 또는 아래에 있는지 확인하고 계절 변경 날짜를 상수로 저장하십시오.

+0

날짜와 시간은 해마다 다릅니다. 다음 연도의 표는 http://en.wikipedia.org/wiki/Equinox를 참조하십시오. –

4

내 신청서의 현재 시즌을 알아야하며 귀하의 게시물을 보았습니다.

그래도 오래된 게시물 인 경우에도 내 대답과 제 코드가 있습니다.

나는 사용자의 좌표를 얻고 사용자가 북반구 (위도 0 ° ~ 90 °) 또는 남반구 (위도 0 ° ~ -90 °)인지를 알기 위도를 비교합니다. . 그 후, 나는 계절에 대한 날짜를 정하고 현재 날짜와 날짜를 비교합니다.

재밌게 :

- (void)currentSeason 
{ 
    NSCalendar* myCalendar = [NSCalendar currentCalendar]; 
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
               fromDate:[NSDate date]]; 
    components.hour = 12; 

    // Current year 
    NSInteger currentYear = components.year; 

    // Today 
    NSDate *today = [NSDate date]; 

    // First Day of the year 
    components.month = 1; 
    components.day = 1; 
    components.year = currentYear; 
    NSDate *firstDayDate = [myCalendar dateFromComponents:components]; 

    // Spring Date 
    components.month = 3; 
    components.day = 21; 
    components.year = currentYear; 
    NSDate *springDate = [myCalendar dateFromComponents:components]; 

    // Summer Date 
    components.month = 6; 
    components.day = 21; 
    components.year = currentYear; 
    NSDate *summerDate = [myCalendar dateFromComponents:components]; 

    // Autumn Date 
    components.month = 9; 
    components.day = 23; 
    components.year = currentYear; 
    NSDate *autumnDate = [myCalendar dateFromComponents:components]; 

    // Winter Date 
    components.month = 12; 
    components.day = 22; 
    components.year = currentYear; 
    NSDate *winterDate = [myCalendar dateFromComponents:components]; 

    // Last Day of the year 
    components.month = 12; 
    components.day = 31; 
    components.year = currentYear; 
    NSDate *lastDayDate = [myCalendar dateFromComponents:components]; 

    // Current Location 
    CLLocationManager *locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coord = [location coordinate]; 

    // North Hemisphere 
    if (coord.latitude > 0) 
    { 
     NSLog(@"North Pole"); 

     // Settings the background image 
     if ([self isDate:today inRangeFirstDate:firstDayDate lastDate:springDate] || [self isDate:today inRangeFirstDate:winterDate lastDate:lastDayDate]) 
     { 
      NSLog(@"Winter"); 
     } 
     else if ([self isDate:today inRangeFirstDate:springDate lastDate:summerDate]) 
     { 
      NSLog(@"Spring"); 
     } 
     else if ([self isDate:today inRangeFirstDate:summerDate lastDate:autumnDate]) 
     { 
      NSLog(@"Summer"); 
     } 
     else if ([self isDate:today inRangeFirstDate:autumnDate lastDate:winterDate]) 
     { 
      NSLog(@"Autumn"); 
     } 
    } 
    else 
    { 
     NSLog(@"South Pole"); 

     // Settings the background image 
     if ([self isDate:today inRangeFirstDate:firstDayDate lastDate:springDate] || [self isDate:today inRangeFirstDate:winterDate lastDate:lastDayDate]) 
     { 
      NSLog(@"Summer"); 
     } 
     else if ([self isDate:today inRangeFirstDate:springDate lastDate:summerDate]) 
     { 
      NSLog(@"Autumn"); 
     } 
     else if ([self isDate:today inRangeFirstDate:summerDate lastDate:autumnDate]) 
     { 
      NSLog(@"Winter"); 
     } 
     else if ([self isDate:today inRangeFirstDate:autumnDate lastDate:winterDate]) 
     { 
      NSLog(@"Spring"); 
     } 
    } 
} 

- (BOOL)isDate:(NSDate *)date inRangeFirstDate:(NSDate *)firstDate lastDate:(NSDate *)lastDate 
{ 
    return ([date compare:firstDate] == NSOrderedDescending) && ([date compare:lastDate] == NSOrderedAscending); 
} 
관련 문제