2016-02-08 78 views
0

我實現了infoWindow來顯示多個標記的數據。但是我所能顯示的全都是通過信息窗口顯示的相同數據。在iOS中顯示相同文本的多個信息窗口

如何顯示與該標記相關的數據,以避免重複?

這裏是我的代碼:

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

回答

2

替換此聲明:

nameLabel.text = nameStr; 

有:使用共享NSString

nameLabel.text = marker.title; 

的問題是,nameStr,它被覆蓋在您的for循環的每次迭代中。因此,所有標籤在最終顯示時共享相同的字符串值。你也可以這樣做:

nameLabel.text = [nameStr copy]; 

,它應該工作 - 但我認爲,在代碼中使用nameStr只是一個以前的一些「黑客」殘餘。

+0

謝謝.... !!! –

+0

[nameStr copy] doest有什麼區別。 –

相關問題