我使用GeoCoder通过发送邮政编码来获取坐标。 GeoCoder返回的第一个响应始终为0.000。正如您在下面看到的那样,我通过地理编码器NSLog传递给weatherCall的值,它始终为0.00000
如果我只是使用相同的方法刷新来设置此处使用的邮政编码,那么第二次就可以了。
以下是我的方法:
-(void)updateWeather
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
zip = [defaults objectForKey:@"zip"];
NSLog(@"%@", zip);
if (zip == NULL) {
[defaults setObject:@"95014" forKey:@"zip"];
[defaults synchronize];
NSLog(@"zip updated to: %@", zip);
}
// check time of last fetch, if more than 0.5 hours, refresh
NSDate *lastFetch = [defaults objectForKey:@"time"];
NSDate *now = [NSDate date];
NSTimeInterval distanceBetweenDates = [now timeIntervalSinceDate:lastFetch];
double secondsInAnHour = 3600;
float hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
NSLog(@"hours between %.2f", hoursBetweenDates);
if (hoursBetweenDates > 0.5 || refreshNow == 1 || isnan(hoursBetweenDates)){
refreshNow = 0;
NSLog(@"hours between was probably nan or hours between was wrong");
// get the coords from the zip
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:zip completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"geo was passed %@ for zip", zip);
cityName = [NSString stringWithFormat:@"%@ is currently",placemark.locality];
[defaults setObject:cityName forKey:@"cityName"];
CLLocation *location = placemark.location;
__block CLLocationCoordinate2D currentLocation = location.coordinate;
[defaults setDouble:currentLocation.latitude forKey:@"lat"];
[defaults setDouble:currentLocation.longitude forKey:@"longi"];
[defaults synchronize];
// call the weather function from end of this block
[self weatherCall];
}]; // end getting coords
// begin cached version below this
} else {
cityName = [defaults objectForKey:@"cityName"];
NSString *currentTemp = [defaults objectForKey:@"currentTemp"];
NSString *desc = [defaults objectForKey:@"desc"];
// update labels to display variables
self.cityLabel.text = cityName;
self.tempLabel.text = currentTemp;
self.descriptionLabel.text = [NSString stringWithFormat:@"%@", desc];
} // end cached version
}
以下是上面提到的weatherCall:
-(void)weatherCall
{
// set coords to defaults
NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
float lat = [[defaults objectForKey:@"lat"] floatValue];
float longi = [[defaults objectForKey:@"longi"] floatValue];
// get current conditions
ForecastKit *forecast = [[ForecastKit alloc] initWithAPIKey:@"a24a3ebfb5aef389141cfcdc479e31b7"];
[forecast getCurrentConditionsForLatitude:lat longitude:longi success:^(NSMutableDictionary *responseDict) {
NSLog(@"first call was sent %f", lat);
NSString *currentTemp = [NSString stringWithFormat:@"%i\u00B0",[responseDict[@"temperature"] intValue]];
[defaults setObject:currentTemp forKey:@"currentTemp"];
NSString *feelsLike = [NSString stringWithFormat:@"%i\u00B0",[responseDict[@"apparentTemperature"] intValue]];
[defaults setObject:feelsLike forKey:@"feelsLike"];
NSString *desc = [responseDict[@"summary"] capitalizedString];
[defaults setObject:desc forKey:@"desc"];
// update labels to display variables
self.cityLabel.text = cityName;
self.tempLabel.text = currentTemp;
self.descriptionLabel.text = [NSString stringWithFormat:@"%@", desc];
// set temp color and hint image based on temp
int temp = [responseDict[@"temperature"] intValue];
[defaults setObject:[NSString stringWithFormat:@"%i",temp] forKey:@"temp"];
if (temp < 32){
self.tempLabel.textColor = [UIColor grayColor];
} else if (temp > 32 && temp < 60){
self.tempLabel.textColor = [UIColor colorWithRed:74/255.0f green:136/255.0f blue:124/255.0f alpha:1.0f];
} else if (temp > 59 && temp < 80) {
self.tempLabel.textColor = [UIColor colorWithRed:138/255.0f green:251/255.0f blue:203/255.0f alpha:1.0f];
} else {
self.tempLabel.textColor = [UIColor colorWithRed:143/255.0f green:87/255.0f blue:67/255.0f alpha:1.0f];
}
[hud hide:YES afterDelay:0.7];
// set fetch time
[defaults setObject:[NSDate date] forKey:@"time"];
[defaults synchronize];
} failure:^(NSError *error){
NSLog(@"Currently %@", error.description);
}];
}