1

无论如何,iOS 中是否可以找到当前位置的当前季节(春季、夏季、冬季、秋季)?

世界不同地区的季节不同。我可能会在澳大利亚经历不同的季节,而在美国的同一时间季节是另一回事。

任何天气 API 或返回那个地方季节的东西?

4

2 回答 2

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);
}
于 2013-09-27T15:21:36.283 回答
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];

这就是你所需要的,我想。确定用户是在赤道之上还是之下,并将季节变化的日期存储为常数。

于 2012-11-14T20:30:15.773 回答