Home Ask Login Register

Developers Planet

Your answer is one click away!

Ammy February 2016

Multiple info windows showing same text in 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        


sergio February 2016

Replace this statement:

nameLabel.text = nameStr;


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".

Post Status

Asked in February 2016
Viewed 1,637 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life