简体   繁体   English

多个信息窗口在iOS中显示相同的文本

[英]Multiple info windows showing same text in iOS

I have implemented infoWindow to show data of my multiple markers. 我已经实现了infoWindow来显示多个标记的数据。 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. 问题是您使用了共享的NSString nameStr ,它在for循环的每次迭代中都会被覆盖。 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". 它应该可以工作-但我认为在您的代码中使用nameStr只是以前的一些“ hack”的残余。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM