gpt4 book ai didi

iphone - 从 plist 数组加载 MapView 注释时出现问题 - Xcode 4.2

转载 作者:行者123 更新时间:2023-11-29 04:43:36 25 4
gpt4 key购买 nike

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

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

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

我认为问题与这个整数有关:for(int i=0; i < [画廊数]; 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>

最佳答案

我不敢说,但是这段代码需要做很多工作才能按您的预期工作。

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

其次,在calloutAccessoryControlTapped中,您将迭代所有图库,并在moredetail View Controller 上设置属性,以便 View Controller 始终显示最后一个的详细信息画廊。

我建议您向 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];

我希望这能让您了解如何使代码正常运行。

关于iphone - 从 plist 数组加载 MapView 注释时出现问题 - Xcode 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9983374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com