Multiple info windows showing same text in iOS - ios

I have implemented infoWindow to show data of my multiple markers. But all i can display is same data in all by info windows .
How to show data which is related to that marker, so that there should be no repetition ?
Here is my code :
for (int i = 0 ; i < jsonDataDict.count; i ++) {
NSDictionary *newDict = [jsonDataDict objectAtIndex:i ];
double latitude = [[newDict valueForKey:#"lat"]doubleValue];
double longitute = [[newDict valueForKey:#"lng"]doubleValue];
nameStr = [newDict valueForKey:#"name"];
countJson = i ;
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude, longitute);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = [newDict valueForKey:#"name"];
// marker.icon = [UIImage imageNamed:#"pin11.png"];
marker.icon = [self image:[UIImage imageNamed:#"pinPopoye.png"] scaledToSize:CGSizeMake(75.0f, 60.0f)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.infoWindowAnchor = CGPointMake(1.1, 0.70);
marker.map = self.mapView;
[self mapView:self.mapView markerInfoWindow:marker];
}
}
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{ UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 25)];
customView.backgroundColor = [UIColor colorWithRed:71.0/255.0 green:65.0/255.0 blue:65.0/255.0 alpha:0.8];
customView.layer.cornerRadius = 5;
customView.layer.masksToBounds = YES;
// Orange Color ==== [UIColor colorWithRed:254.0/255.0 green:165.0/255.0 blue:4.0/255.0 alpha:0.5];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 10)];
nameLabel.text = nameStr;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:nameLabel];
return customView;
}

Replace this statement:
nameLabel.text = nameStr;
with:
nameLabel.text = marker.title;
The problem is your using a shared NSString, nameStr, which gets overwritten at each iteration of your for loop. So, all label share the same string value when they are finally displayed. You could also do:
nameLabel.text = [nameStr copy];
and it should work -- but I think that using nameStr in your code was just a remnant of some previous "hack".

Related

not able to tap on MKPinAnnotationView Second Time

I have created Custom annotation by using this code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *const AnnotatioViewReuseID = #"AnnotatioViewReuseID";
// MKAnnotationView
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotatioViewReuseID];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotatioViewReuseID];
}
if ([annotation isKindOfClass:[FBAnnotationCluster class]]) {
FBAnnotationCluster *cluster = (FBAnnotationCluster *)annotation;
cluster.title = [NSString stringWithFormat:#"%lu", (unsigned long)cluster.annotations.count];
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor colorWithRed:33.0/255.0 green:191.0/255.0 blue:133.0/255.0 alpha:1.0];
UILabel *label = [[UILabel alloc]init];
label.text = cluster.title;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
label.font = font;
label.frame = CGRectMake(0, 0, [self widthOfString:label.text]+20, [self widthOfString:label.text]+20);
view.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.width) ;
view.layer.cornerRadius = view.frame.size.height/2;
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 2.0;
view.clipsToBounds = true;
[view addSubview:label];
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
[annotationView addSubview:view];
annotationView.enabled = YES;
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
annotationView.pinTintColor = [UIColor clearColor];
} else {
annotationView.pinTintColor = [UIColor clearColor];
annotationView.layer.borderColor = [UIColor clearColor].CGColor;
annotationView.layer.borderWidth = 0.0;
FBAnnotation *a = (FBAnnotation*)annotation;
NSLog(#"amount is %f",a.amount);
UIImage *image = [UIImage imageNamed:#"icon-marker-select"];
UIImageView *imgView = [[UIImageView alloc]init];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumFractionDigits = 2;
NSString *result = [formatter stringFromNumber:[NSNumber numberWithDouble:a.amount]];
NSString *strData = [NSString stringWithFormat:#"%#%#",a.currency,result];
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : [UIColor whiteColor]
};
CGSize textSize = [strData sizeWithAttributes:attributes];
CGRect textRect = CGRectMake(5, (image.size.height-textSize.height)/2 - 2, textSize.width , textSize.height);
UILabel *textLable = [[UILabel alloc]initWithFrame:textRect];
textLable.textColor = [UIColor whiteColor];
textLable.font = font;
textLable.text = strData;
UIImage *lightSymImg = [UIImage imageNamed:#"icon_lightning"];
UIImageView *lightImage = [[UIImageView alloc]init];
lightImage.image = lightSymImg;
CGRect imgRect = CGRectMake(textRect.origin.x+textRect.size.width, (image.size.height-lightSymImg.size.height)/2 - 2, lightSymImg.size.width,lightSymImg.size.height);
lightImage.frame = imgRect;
imgView.frame = CGRectMake(0, 0, imgRect.size.width + imgRect.origin.x+5, image.size.height);
imgView.image = image;
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
annotationView.annotation = annotation;
[annotationView addSubview:imgView];
[annotationView addSubview:textLable];
[annotationView addSubview:lightImage];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
}
return annotationView;
}
And in didSelect method i have written this code for Present a view Controller
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"didSelectAnnotationView");
if ([view.annotation isKindOfClass:[FBAnnotationCluster class]]) {
NSLog(#"FBAnnotationCluster select annotation");
}
else{
SpotDetailVC *locDetailVC = [[UIStoryboard storyboardWithName:#"SpotDetail" bundle:nil] instantiateViewControllerWithIdentifier:#"SpotDetailVC"];
[self presentViewController:navController animated:YES completion:nil];
}
}
Now When i click on annotation for the first Time it works but after dismiss that view again click on Same annotation it does not works.
Please help, i know it's Silly mistake but i am not able to configure out that one.

Current Location SKPulseAnimation does not work

I am doing one function, when users pressed a button, map will indicate users' current location by on pulse animation annotation.
SKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude);
region.zoomLevel = 14;
self.mapView.visibleRegion = region;
SKAnnotation *CurrentLocationAnnotation = [SKAnnotation annotation];
CurrentLocationAnnotation.identifier = 1;
//CurrentLocationAnnotation.annotationType = SKAnnotationTypeMarker;
CurrentLocationAnnotation.location = CLLocationCoordinate2DMake(_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude);
SKAnimationSettings *CurrentLocationanimationSettings = [SKAnimationSettings animationSettings];
CurrentLocationanimationSettings.animationType = SKPulseAnimation;
CurrentLocationanimationSettings.animationEasingType = SKAnimationEaseLinear;
CurrentLocationanimationSettings.duration = 7000;
[self.mapView addAnnotation:CurrentLocationAnnotation withAnimationSettings:CurrentLocationanimationSettings];
I tried it on simulator and iphone as well, both of them don't work at all.
How can i fix it? thanks in advance
CLLocationCoordinate2D cordinate;
- (void)viewDidLoad {
[super viewDidLoad];
self.mapview.delegate=self;
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
[self.mapview setMapType:MKMapTypeSatellite];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 122, 21)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.3];
label.text = #"title";
//create our view for the image
UIImageView *coloredView1 = [[UIImageView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
coloredView1.image = [UIImage imageNamed:#"demo.png"];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
view1.backgroundColor = [UIColor clearColor];
[view1 addSubview:label];
[view1 addSubview:coloredView1];
//create our view for the background image
UIImageView *coloredView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0.0, 136, 110)];
coloredView2.image = [UIImage imageNamed:#"mapMarker"];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
view1.backgroundColor = [UIColor clearColor];
[view2 addSubview:coloredView2];
[view2 addSubview:view1];
//create our view for the background image
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0.0, 136, 200)];
view3.backgroundColor = [UIColor clearColor];
[view3 addSubview:view2];
SKAnnotationView *annotationView = [[SKAnnotationView alloc] initWithView:customAnnotationView reuseIdentifier:#"reusableIdentifier"];
SKAnnotation *annotation = [SKAnnotation annotation];
annotation.identifier = 123456;
annotation.location = region.center;
annotation.annotationView = annotationView;
[self.mapView addAnnotation:annotation withAnimationSettings:[SKAnimationSettings animationSettings]];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *identifier = #"myAnnotation";
DraggableAnnotationView * annotationView = (DraggableAnnotationView *)[self.mapview dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[DraggableAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:#"ic_map_pin.png"];
}
else
{
annotationView.annotation = annotation;
}
annotationView.delegate = self;
return annotationView;
}
if this code helpful for you then give your vote ... thank you

UITapGestureRecognizer not working, Objective-C

I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;

how to show multiple info window of GMS mapView?

I want to add multiple info windows to appear at the same time with out tapping on the marker .
How to implement this ?
Here is my code to add multiple markers and adding a infoWindow?
for (int i = 0 ; i < jsonDataDict.count; i ++) {
NSDictionary *newDict = [jsonDataDict objectAtIndex:i ];
double latitude = [[newDict valueForKey:#"lat"]doubleValue];
double longitute = [[newDict valueForKey:#"lng"]doubleValue];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude, longitute);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
nameOfPlace = [newDict valueForKey:#"name"];
// distanceOfPlace = [newDict valueForKey:#"distance"];
marker.icon = [self image:[UIImage imageNamed:#"pinPopoye.png"] scaledToSize:CGSizeMake(75.0f, 60.0f)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.infoWindowAnchor = CGPointMake(1.1, 0.70);
marker.map = self.mapView;
[self.mapView setSelectedMarker:marker];
//[self mapView:self.mapView markerInfoWindow:marker];
}
}
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 25)];
customView.backgroundColor = [UIColor colorWithRed:71.0/255.0 green:65.0/255.0 blue:65.0/255.0 alpha:0.8];
customView.layer.cornerRadius = 5;
customView.layer.masksToBounds = YES;
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 10)];
nameLabel.text = nameOfPlace;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:nameLabel];
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, 60, 10)];
distanceLabel.text = distanceOfPlace;
distanceLabel.textColor = [UIColor whiteColor];
distanceLabel.textAlignment = NSTextAlignmentCenter;
distanceLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:distanceLabel];
return customView;
}

iOS - Reuseable UIView Subclass

I am trying to build a few different UIView subclasses to display information to my app users.
One problem I am having is being able to call the view more than once.
Here is the testing call:
if(rewardsView==nil){
rewardsView = [[RewardsView alloc] init];
[rewardsView showRewardType:RewardsAttack withXP:100 withZBucks:10 isCritical:NO];
}else{
[rewardsView showRewardType:RewardsAttack withXP:200 withZBucks:20 isCritical:NO];
}
Here is the Code to initialize and display:
- (id)init
{
self = [super init];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
//Z-Bucks
zBucksIcon = [UIImage imageNamed:#"rewards-z-bucks"];
zBucksView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[zBucksView setImage:zBucksIcon];
zBucksView.alpha = 0;
zBucksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, zBucksIcon.size.height, zBucksIcon.size.width,25)];
zBucksLabel.backgroundColor = [UIColor clearColor];
zBucksLabel.textAlignment = NSTextAlignmentCenter;
zBucksLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
zBucksLabel.textColor = [UIColor whiteColor];
zBucksLabel.shadowColor = [UIColor blackColor];
zBucksLabel.shadowOffset = CGSizeMake(1, 1);
[zBucksView addSubview:zBucksLabel];
//XP
xpIcon = [UIImage imageNamed:#"rewards-xp"];
xpView = [[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
[xpView setImage:xpIcon];
xpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, xpIcon.size.height, xpIcon.size.width,25)];
xpLabel.backgroundColor = [UIColor clearColor];
xpLabel.textAlignment = NSTextAlignmentCenter;
xpLabel.font = [UIFont fontWithName:#"Open Sans Condensed" size:24];
xpLabel.textColor = [UIColor blackColor];
xpLabel.shadowColor = [UIColor whiteColor];
xpLabel.shadowOffset = CGSizeMake(1, 1);
[xpView addSubview:xpLabel];
xpView.alpha = 0;
[self addSubview:zBucksView];
[self addSubview:xpView];
}
return self;
}
-(void)showRewardType:(RewardsType)type withXP:(int)xp withZBucks:(int)zBucks isCritical:(BOOL)critical{
//Set Values
zBucksLabel.text = [NSString stringWithFormat:#"+%i",zBucks];
xpLabel.text = [NSString stringWithFormat:#"+%i",xp];
if(type==RewardsAttack){
}
if(type==RewardsHeal){
}
if(type==RewardsDaily){
}
UIWindow *window = [appDelegate window];
[window addSubview:self];
[self animateRewards];
}
Here is the code that dismisses (after some animation functions are called, not shown here):
-(void)dismissRewards{
[self removeFromSuperview];
//Reset Views
zBucksView.alpha = 0;
xpView.alpha = 0;
[zBucksView setFrame:CGRectMake(DEVICE_WIDTH/4-zBucksIcon.size.width/2, DEVICE_HEIGHT/2-zBucksIcon.size.height/2-12, zBucksIcon.size.width, zBucksIcon.size.height+25)];
[xpView setFrame:CGRectMake(DEVICE_WIDTH/4*3-xpIcon.size.width/2, DEVICE_HEIGHT/2-xpIcon.size.height/2-12, xpIcon.size.width, xpIcon.size.height+25)];
}

Resources