问题
I am trying to display a title in each markers info window that will display the name of the event. Right now I only can display the very last elements name in the JSON array, and am unsure how to make the others their event title. Any help is appreciated, thanks
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:cityLattitude
longitude:cityLongitude
zoom:7];
[self.mapView animateToCameraPosition:camera];
//GMSMapView *mapView; //= [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView animateToCameraPosition:camera];
mapView.indoorEnabled = NO;
mapView.mapType = kGMSTypeNormal;
viewFormap = mapView;
mapView.accessibilityElementsHidden = NO;
mapView.myLocationEnabled = YES;
NSLog(@"User's location: %@", mapView.myLocation);
mapView.delegate = self;
GMSMarker * marker = [[GMSMarker alloc]init];
marker.position = CLLocationCoordinate2DMake(cityLattitude, cityLattitude );
marker.map = mapView;
[mapView animateToCameraPosition:camera];
}
To display annotations i used the following code:
-(void)plotMutliplePinsOnMap//:(NSArray *)MapDataArray { for(int i=0;i<[MapDataArray count];i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
// Set label properties as required including marker Name
UIImageView *markerIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(35, 10, 30, 50)];
// Set image properties as required including image which will be displayed as marker
UIView *markerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[markerView addSubview:label];
[markerView addSubview:markerIconImageView];
//GMSMapView *mapView = [[GMSMapView alloc]init];
viewFormap = mapView;
latitude = [[[MapDataArray objectAtIndex:i]valueForKey:@"latitude"] doubleValue];
longitude = [[[MapDataArray objectAtIndex:i]valueForKey:@"longitude"] doubleValue];
eventTypeStr = [[MapDataArray objectAtIndex:i]objectForKey:@"is_paid"];
NSString * price = [[MapDataArray objectAtIndex:i] objectForKey:@"evt_price"];
NSString * Mainprice = [NSString stringWithFormat:@"$%@",price];
NSString * name = [[MapDataArray objectAtIndex:i] objectForKey:@"evt_title"];
NSLog(@"Name is %@", name);
for(int i=0;i<[MapDataArray count];i++){
GMSMarker * marker = [[GMSMarker alloc] init];
if ([eventTypeStr isEqualToString:@"0"])
{
NSLog(@"Eventtype Array is %@",eventTypeStr);
NSLog(@"Event Type is %@",eventTypeStr);
marker.icon = [UIImage imageNamed:@"smiley.png"];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = name;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
else if ([eventTypeStr isEqualToString:@"1"])
{
NSLog(@"Event Type is %@",eventTypeStr);
//marker.icon = [UIImage imageNamed:@"dollar1.png"];
marker.icon = [self drawText:price inImage:[UIImage imageNamed:@"markertt1.png"]];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = price;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
else if ([eventTypeStr isEqualToString:@"2"])
{
NSLog(@"Event Type is %@",eventTypeStr);
marker.icon = [UIImage imageNamed:@"donation.png"];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = name;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
}
}
}
来源:https://stackoverflow.com/questions/62134167/ios-marker-title-display-only-last-index-of-the-array-for-multiple-markers