在这一点上磕磕绊绊,我已经用尽了很多时间。我看了几个其他的问题和答案,但似乎无法解决这个问题。现在代码只是使所有注释都相同,这是我的数组中的最后一个图像。基本上,我想根据字典中的图像更改每个图钉的注释图像。因此,地图上的所有60个引脚都将是不同的图像。这是我设置注释的方法:
//Remove any existing custom annotations but not the user location blue dot.
for (id<MKAnnotation> annotation in mapView.annotations)
{
if ([annotation isKindOfClass:[MapPoint class]])
{
[mapView removeAnnotation:annotation];
}
}
NSArray *dict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FastFood" ofType:@"plist"]];
//Access name value in dictionary, store to array
// NSArray *nameFromPlist = [dict valueForKey:@"name"];
//Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++)
{
int currentIndex = -1;
for (NSDictionary *plistname in [dict valueForKey:@"name"])//nameFromPlist[@"name"])
{
currentIndex++;
NSPredicate *inBothLists=[NSPredicate predicateWithFormat:@"Self CONTAINS[c] %@", plistname];
BOOL result = [inBothLists evaluateWithObject:[[data objectAtIndex:i] objectForKey:@"name"]];
if (result == YES)
{
//Retrieve the NSDictionary object in each index of the array.
NSDictionary* place = [data objectAtIndex:i];
NSDictionary* image = [dict objectAtIndex:currentIndex];
//There is a specific NSDictionary object that gives us location info.
NSDictionary *geo = [place objectForKey:@"geometry"];
imgRestLogo = nil;
imgRestLogo = [UIImage imageNamed:[image objectForKey:@"image"]];
// NSString *imgtest= [image objectForKey:@"image"];
// NSLog(@"Image Filename is: %@ " , imgtest);
//Get our name and address info for adding to a pin.
NSString *name=[place objectForKey:@"name"];
NSString *vicinity=[place objectForKey:@"vicinity"];
//Get the lat and long for the location.
NSDictionary *loc = [geo objectForKey:@"location"];
//Create a special variable to hold this coordinate info.
CLLocationCoordinate2D placeCoord;
//Set the lat and long.
placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];
//Create a new annotiation.
MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:placeCoord];
[mapView addAnnotation:placeObject];
}
这是我的viewForAnnotation方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//Define our reuse indentifier.
static NSString *identifier = @"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
UIImage *image = imgRestLogo; //[UIImage imageNamed:@"GMPin.png"];
annotationView.image = image;
return annotationView;
}
return nil;
}
提前致谢!
答案 0 :(得分:0)
我的建议是首先使用imageName属性对MKAnnotation进行子类化,假设您有一个名为MYAnnotation的类
@interface MYAnnotation : NSObject
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *subtitle;
@property (copy, nonatomic) NSString *imageName;
@end
然后添加您的图像名称。 (注意:获取图像名称的方法取决于plist文件的结构。)
NSDictionary *imageName = [dict objectAtIndex:currentIndex];
annotation.imageName = imageName;
在委托中,按以下方式设置图像。
MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
NSString *imageName = ((MYAnnotation *)annotation).imageName;
UIImage *image = [UIImage imageNamed:imageName];
annotationView.image = image;
return annotationView;