【问题标题】:Multiple info windows showing same text in iOS在 iOS 中显示相同文本的多个信息窗口
【发布时间】:2016-05-18 03:05:14
【问题描述】:

我已经实现了 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];


      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;
}

【问题讨论】:

    标签: ios objective-c google-maps google-maps-markers infowindow


    【解决方案1】:

    替换此语句:

    nameLabel.text = nameStr;
    

    与:

    nameLabel.text = marker.title;
    

    问题在于您使用了共享的NSStringnameStr,它在您的for 循环的每次迭代中都会被覆盖。因此,所有标签在最终显示时共享相同的字符串值。你也可以这样做:

    nameLabel.text = [nameStr copy];
    

    它应该可以工作——但我认为在你的代码中使用nameStr 只是之前一些“hack”的残余。

    【讨论】:

    • [nameStr copy ] 没有任何区别。
    猜你喜欢
    • 2013-06-08
    • 2017-03-15
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2012-10-31
    • 2019-01-10
    • 2014-07-21
    相关资源
    最近更新 更多