问题从plist数组加载MapView注释 - Xcode 4.2

时间:2012-04-02 20:29:49

标签: iphone ios xcode xcode4.2 mapkit

我正在研究一个在地图上显示画廊列表的iPhone应用程序。画廊列表来自从网上下载的plist。位置坐标正确加载到地图中,以及每个位置的标题和副标题。

我还可以从plist中加载注释视图图像的图像。

我遇到的问题是注释视图图像和详细视图页面都加载了plist数组中最后一个位置的信息,而不是加载与正确位置信息相对应的信息。

我认为问题与这个整数有关: for(int i = 0; i< [galleries count]; i ++)

但老实说我不确定。

非常感谢任何帮助!

以下是代码:

MapAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface MapAnnotation : NSObject <MKAnnotation>{

CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subTitle;

}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

MapAnnotation.m

#import "MapAnnotation.h"

@implementation MapAnnotation
@synthesize coordinate, title, subtitle;

@end

MapViewViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>{

NSMutableArray *galleries;
MKMapView *_MapView;
MKAnnotationView *Ann;

}


@property (nonatomic, retain) IBOutlet MKMapView *_MapView;

@property (nonatomic, retain) IBOutlet MKAnnotationView *Ann;

@property (nonatomic, retain) NSArray *addressArray;

@property (nonatomic, retain) NSMutableArray *galleries;

@property (nonatomic, retain) NSString *imageUrl;

@property (nonatomic, retain) NSData *imagedata;


-(IBAction)getlocation;

- (void)loadAnnotations;


@end

MapViewViewController.m

#import "MapViewViewController.h"
#import "MapAnnotation.h"
#import "MoredetailViewController.h"


@implementation MapViewViewController
@synthesize _MapView, Ann;
@synthesize galleries, addressArray;
@synthesize imagedata, imageUrl;


- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[galleries removeAllObjects];

addressArray=[[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.jimirobinson.com/plist/MapInformation.plist"]];    

galleries = [[NSMutableArray alloc] initWithArray:addressArray];

[self loadAnnotations]; 

}


- (void)viewDidLoad
{
[super viewDidLoad];

MKCoordinateRegion region;
region.center.latitude = 39.952133;
region.center.longitude = -75.144264;
region.span.longitudeDelta = 0.005;
region.span.latitudeDelta = 0.001;

[_MapView setRegion:region animated:YES];
}


-(IBAction)getlocation {
_MapView.showsUserLocation = YES;
}


- (void)loadAnnotations {

CLLocationCoordinate2D pinCoordinates;

for (int i = 0; i < [galleries count]; i++)
{

    MapAnnotation *myAnnotations = [[MapAnnotation alloc] init];

    pinCoordinates.latitude = [[[galleries objectAtIndex:i] objectForKey:@"latitude"] floatValue];
    pinCoordinates.longitude = [[[galleries objectAtIndex:i] objectForKey:@"longitude"] floatValue];
    [myAnnotations setTitle:[[galleries objectAtIndex:i] objectForKey:@"annotation_title"]];
    [myAnnotations setSubtitle:[[galleries objectAtIndex:i] objectForKey:@"annotation_subtitle"]];

    imageUrl = [[galleries objectAtIndex:i] objectForKey:@"map_pin_image_url"];
    imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

    [myAnnotations setCoordinate:pinCoordinates];
    [_MapView addAnnotation:myAnnotations];

}

                    }


- (MKAnnotationView *)mapView :(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"AddrAnnot"];

annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AddrAnnot"];

    if (_MapView.userLocation == annotation)
            {
            return nil;
            }

annotationView.image = [UIImage imageWithData:imagedata];          
annotationView.canShowCallout = YES;
[annotationView setBackgroundColor:[UIColor clearColor]];
[annotationView setFrame:CGRectMake(0, 0, 30, 30)];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.leftCalloutAccessoryView =  [[UIImageView  alloc] initWithImage:[UIImage imageNamed:@"flag1.png"]];


[_MapView addAnnotation:annotation];
return annotationView;

NSLog(@"%s: annotationView=%@", __func__, annotationView);

}




- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

MoredetailViewController *moredetail = [self.storyboard instantiateViewControllerWithIdentifier:@"TextPage"];
[self.navigationController pushViewController:moredetail animated:YES];


for(int i=0; i < [galleries count]; i++)        
{
    moredetail.MoredetailTitle.text = [[galleries objectAtIndex:i] objectForKey:@"annotation_title"];  

    moredetail.MoredetailDescription.text = [[galleries objectAtIndex:i] objectForKey:@"annotation_subtitle"];
    NSLog(@"%d", i);


    NSString *image = [[galleries objectAtIndex:i] objectForKey:@"map_pin_image_url"];  
    NSData *dataz = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
    moredetail.detailImage.image = [UIImage imageWithData:dataz];
} 


NSLog(@"I've been tapped");

}



- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 

NSLog(@"%s: galleries=%@", __func__, galleries);

}


- (void)viewDidUnload
{
    [super viewDidUnload];
}


@end

MapInformation.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
    <key>title</key>
    <string>Gallery One</string>
    <key>annotation_title</key>
    <string>Gallery One Title</string>
    <key>annotation_subtitle</key>
    <string>Gallery One Subtitle</string>
    <key>map_pin_image_url</key>
    <string>http://www.jimirobinson.com/plist/flag1.png</string>
    <key>latitude</key>
    <real>39.952827</real>
    <key>longitude</key>
    <real>-75.145203</real>
</dict>
<dict>
    <key>title</key>
    <string>Gallery Two</string>
    <key>annotation_title</key>
    <string>Gallery Two Title</string>
    <key>annotation_subtitle</key>
    <string>Gallery Two Subtitle</string>
    <key>map_pin_image_url</key>
    <string>http://www.jimirobinson.com/plist/flag2.png</string>
    <key>latitude</key>
    <real>39.952184</real>
    <key>longitude</key>
    <real>-75.145368</real>
</dict>
<dict>
    <key>title</key>
    <string>Gallery Three</string>
    <key>annotation_title</key>
    <string>Gallery Three Title</string>
    <key>annotation_subtitle</key>
    <string>Gallery Three Subtitle</string>
    <key>map_pin_image_url</key>
    <string>http://www.jimirobinson.com/plist/flag3.png</string>
    <key>latitude</key>
    <real>39.951135</real>
    <key>longitude</key>
    <real>-75.143368</real>
</dict>
</array>
</plist>

1 个答案:

答案 0 :(得分:1)

我不敢说,但这段代码需要大量工作才能按预期工作。

首先,imagedata是一个实例变量,您将其设置为最后一个注释的图像数据(在loadAnnotations中)。 viewForAnnotation然后使用imagedata作为图片,因此它将始终设置为最后一个注释的图像。

其次,在calloutAccessoryControlTapped中,您正在迭代所有图库,并在moredetail视图控制器上设置属性,以便视图控制器始终显示最后一个图库的详细信息。

我建议您为MapAnnotation类添加更多属性,例如imageUrl。然后,您可以在创建注释时设置此属性。在viewForAnnotation中,您可以执行此操作:

if ([annotation isKindOfClass:[MapAnnotation class]]) 
{
      MapAnnotation *mapAnnotation = (MapAnnotation *)annotation;
      annotationView.image = [NSData dataWithContentsOfURL:[NSURL URLWithString:[mapAnnotation imageURL]];

calloutAccessoryControlTapped方法中,您可以执行此操作:

MapAnnotation *mapAnnotationPressed = (MapAnnotation *)view.annotation;
moredetail.MoredetailTitle.text = [mapAnnotationPressed title];

我希望这可以让您了解如何使代码正常工作。