Created
July 4, 2013 20:32
-
-
Save gscalzo/5930075 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { | |
static NSString *identifier = @"PPSpot"; | |
if ([annotation isKindOfClass:[PPSpot class]]) { | |
PPAnnotationView *annotationView = (PPAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; | |
annotationView = [[PPAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; | |
annotationView.parent = self; | |
NSString *text = @""; | |
float tValue = [[NSUserDefaults standardUserDefaults] boolForKey:kUnitDefault] ? | |
[[PPAppDelegate sharedInstance] celsiusToFahrenheit:[(PPSpot*)annotationView.annotation spot].temperature] : | |
[(PPSpot*)annotationView.annotation spot].temperature; | |
switch (type) { | |
case MapTypeTemperature: | |
text = [NSString stringWithFormat:@"%.1f°", tValue]; | |
break; | |
case MapTypeHumidity: | |
text = [NSString stringWithFormat:@"%.0f%@", [(PPSpot*)annotationView.annotation spot].humidity, @"%"]; | |
break; | |
case MapTypePressure: | |
text = [NSString stringWithFormat:@"%.0f", [(PPSpot*)annotationView.annotation spot].pressure]; | |
break; | |
default: | |
break; | |
} | |
if ([(PPSpot*)annotationView.annotation spot].deviceId.length == 0) { | |
// CLUSTER | |
[annotationView setType:AnnotationTypeCluster]; | |
CGRect f = annotationView.frame; | |
f.size.width = 60; | |
f.size.height = 60; | |
annotationView.frame = f; | |
if (type == MapTypeForecast) { | |
__block CLLocationCoordinate2D coordinate = annotation.coordinate; | |
__block NSDate *date = [(PPSpot*)annotationView.annotation spot].date; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
[PPWeatherServer retrieveForecastInCoordinate:coordinate success:^(NSDictionary *spots) { | |
@try { | |
NSArray *weatherBeans = [spots objectForKey:@"weatherBeans"]; | |
NSDictionary *forecastDict = [weatherBeans objectAtIndex:0]; | |
NSLog(@"%s forecastDict: %@", __PRETTY_FUNCTION__, forecastDict); | |
NSString *sunrise = [forecastDict objectForKey:@"sr"]; | |
NSString *sunset = [forecastDict objectForKey:@"ss"]; | |
NSDate *sunriseDate = [[NSDate dateFromString:sunrise withFormat:@"hh:mm:ss"] localizedDate]; | |
NSDate *sunsetDate = [[NSDate dateFromString:sunset withFormat:@"HH:mm:ss"] localizedDate]; | |
NSDate *a = [NSDate dateWithYear:[date year] month:[date month] day:[date day] hours:[sunriseDate GMThours] minutes:[sunriseDate minutes] seconds:[sunriseDate seconds]]; | |
NSDate *b = [NSDate dateWithYear:[date year] month:[date month] day:[date day] hours:[sunsetDate GMThours] minutes:[sunsetDate minutes] seconds:[sunsetDate seconds]]; | |
BOOL isDay = NO; | |
// check se giorno o notte | |
if ([NSDate date:date isBetweenDate:a andDate:b]) { | |
isDay = YES; | |
} else { | |
isDay = NO; | |
} | |
NSString *imageName = @""; | |
NSNumber *wcNumber = [forecastDict objectForKey:@"wc"]; | |
NSDictionary *values = [[PPAppDelegate sharedInstance] forecastValues:wcNumber isDay:isDay]; | |
imageName = [values objectForKey:@"imageName"]; | |
// possibile prefix se "clear" o "partly" | |
NSString *prefix = @""; | |
if ([imageName isEqualToString:@"clear"] || [imageName isEqualToString:@"partly"]) { | |
prefix = isDay ? @"day_" : @"night_"; | |
} | |
imageName = [NSString stringWithFormat:@"%@cluster_%@", prefix, imageName]; | |
UIImage *forecastImage = [UIImage imageNamed:imageName]; | |
annotationView.image = forecastImage; | |
UIImageView *pinView = [[UIImageView alloc] initWithImage:forecastImage]; | |
pinView.frame = CGRectMake(0, 0, 60, 60); | |
[annotationView addSubview:pinView]; | |
} | |
@catch (NSException *exception) { | |
NSLog(@"%@", exception); | |
} | |
} failure:^(NSError *error) { | |
// | |
NSLogE(@"retrievingError: %@",error); | |
}]; | |
}); | |
}); | |
} else { | |
UIImageView *pinView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cluster"]]; | |
pinView.frame = CGRectMake(0, 0, 60, 60); | |
[annotationView addSubview:pinView]; | |
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; | |
lbl.backgroundColor = [UIColor clearColor]; | |
lbl.textColor = UIColorFromRGB(0x333333); | |
lbl.textAlignment = NSTextAlignmentCenter; | |
lbl.font = [UIFont boldSystemFontOfSize:16]; | |
lbl.text = text; | |
[annotationView addSubview:lbl]; | |
} | |
} else if([(PPSpot*)annotationView.annotation spot].howMany == -1) { | |
} else { | |
// PIN | |
CGRect f = annotationView.frame; | |
f.size.width = 40; | |
f.size.height = 42.5; | |
annotationView.frame = f; | |
[annotationView setType:AnnotationTypePin]; | |
if (type == MapTypeForecast) { | |
// FORECAST | |
__block CLLocationCoordinate2D coordinate = annotation.coordinate; | |
__block NSDate *date = [(PPSpot*)annotationView.annotation spot].date; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
[PPWeatherServer retrieveForecastInCoordinate:coordinate success:^(NSDictionary *spots) { | |
@try { | |
NSArray *weatherBeans = [spots objectForKey:@"weatherBeans"]; | |
NSDictionary *forecastDict = [weatherBeans objectAtIndex:0]; | |
NSLog(@"%s forecastDict: %@", __PRETTY_FUNCTION__, forecastDict); | |
NSString *sunrise = [forecastDict objectForKey:@"sr"]; | |
NSString *sunset = [forecastDict objectForKey:@"ss"]; | |
NSDate *sunriseDate = [[NSDate dateFromString:sunrise withFormat:@"hh:mm:ss"] localizedDate]; | |
NSDate *sunsetDate = [[NSDate dateFromString:sunset withFormat:@"HH:mm:ss"] localizedDate]; | |
NSDate *a = [NSDate dateWithYear:[date year] month:[date month] day:[date day] hours:[sunriseDate GMThours] minutes:[sunriseDate minutes] seconds:[sunriseDate seconds]]; | |
NSDate *b = [NSDate dateWithYear:[date year] month:[date month] day:[date day] hours:[sunsetDate GMThours] minutes:[sunsetDate minutes] seconds:[sunsetDate seconds]]; | |
BOOL isDay = NO; | |
// check se giorno o notte | |
if ([NSDate date:date isBetweenDate:a andDate:b]) { | |
isDay = YES; | |
} else { | |
isDay = NO; | |
} | |
NSString *imageName = @""; | |
NSNumber *wcNumber = [forecastDict objectForKey:@"wc"]; | |
NSDictionary *values = [[PPAppDelegate sharedInstance] forecastValues:wcNumber isDay:isDay]; | |
imageName = [values objectForKey:@"imageName"]; | |
// possibile prefix se "clear" o "partly" | |
NSString *prefix = @""; | |
if ([imageName isEqualToString:@"clear"] || [imageName isEqualToString:@"partly"]) { | |
prefix = isDay ? @"day_" : @"night_"; | |
} | |
imageName = [NSString stringWithFormat:@"%@pin_%@", prefix, imageName]; | |
UIImage *forecastImage = [UIImage imageNamed:imageName]; | |
UIImageView *pinView = [[UIImageView alloc] initWithImage:forecastImage]; | |
pinView.frame = CGRectMake(0, 0, 40, 42.5f); | |
[annotationView addSubview:pinView]; | |
} | |
@catch (NSException *exception) { | |
NSLogE(@"%@", exception); | |
} | |
} failure:^(NSError *error) { | |
// | |
NSLogE(@"retrievingError: %@",error); | |
}]; | |
}); | |
}); | |
} else { | |
UIImageView *pinView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin"]]; | |
pinView.frame = CGRectMake(0, 0, 40, 42.5f); | |
[annotationView addSubview:pinView]; | |
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(9, 6, 26, 26)]; | |
lbl.backgroundColor = [UIColor clearColor]; | |
lbl.textColor = UIColorFromRGB(0x333333); | |
lbl.textAlignment = NSTextAlignmentCenter; | |
lbl.font = [UIFont boldSystemFontOfSize:10]; | |
lbl.text = text; | |
[annotationView addSubview:lbl]; | |
} | |
annotationView.centerOffset = CGPointMake(0, -10); | |
} | |
NSDate *date = [(PPSpot*)annotationView.annotation spot].date; | |
int hours = [NSDate howManyHoursHavePast:date today:[[NSDate date] localizedDate]]; | |
if (hours <= 1) { | |
annotationView.alpha = 1.0; | |
} else if (hours <= 3) { | |
annotationView.alpha = 0.8; | |
} else { | |
annotationView.alpha = 0.5; | |
} | |
return annotationView; | |
} | |
else if ([annotation isKindOfClass:[PPSearchedSpot class]]) { | |
static NSString *CodeAnnotationView = @"SearchedSpotAnnotationView"; | |
MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:CodeAnnotationView]; | |
if (!view) { | |
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:CodeAnnotationView]; | |
} | |
view.image = [UIImage imageNamed:@"search_map_pin"]; | |
view.canShowCallout = YES; | |
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; | |
view.rightCalloutAccessoryView = rightButton; | |
return view; | |
} | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment