使用 NSDictionary 在地图上添加注释?
感谢您提前提供的帮助...在过去的几个小时里,这个帮助一直困扰着我。
我目前正在提取 JSON feed 并将其存储在 NSDictionary & 中。 NSArray。我试图为每个被拉入的项目添加注释(时间、类型、纬度和经度)。到目前为止,我可以从数组中提取每个值,并在控制台中使用“for”重复它们(请参见下面的代码)。
如何将这些值存储为注释?任何帮助都会很棒。
下面是我失败的尝试...
- (void)viewDidLoad {
[super viewDidLoad];
// Download JSON Feed
NSDictionary *feed = [self downloadFeed];
NSArray *streams = (NSArray *)[feed valueForKey:@"stream"];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 29.719023;
region.center.longitude = -114.157110;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
int Info;
for (Info = 0; Info < streams.count; Info++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:Info];
NSLog(@"Time: %@", [stream valueForKey:@"TheTime"]);
NSLog(@"Type: %@", [stream valueForKey:@"Type"]);
NSLog(@"Longitude: %@", [stream valueForKey:@"Longitude"]);
NSLog(@"Latitude: %@", [stream valueForKey:@"Latitude"]);
NSString *getLat = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Latitude"]];
NSString *getLong = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Longitude"]];
NSString *getCoord = [[NSString alloc] initWithFormat: @"{%@,%@}", getLat, getLong];
getCoordinates = getCoord;
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"%@", [stream valueForKey:@"TheTime"];
ann.subtitle = @"%@", [stream valueForKey:@"Type"];
ann.coordinate = getCoordinates;
[mapView addAnnotation:ann];
}
}
的代码
这是 DisplayMap DisplayMap.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end
,现在是 DisplayMap.m
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(void)dealloc{
[title release];
[super dealloc];
}
@end
Thanks for the help in advance... this one has been killing me for the past couple of hours.
I am currently pulling in a JSON feed and storing it in a NSDictionary & NSArray. I'm trying to add an annotation for each item being pulled in (time, type, latitude, and longitude). So far, I can extract each value from the Array and have them all repeat with a "for" in the console (see code below).
How to I store these values as an annotation? Any help would be great.
Below is my failed attempt...
- (void)viewDidLoad {
[super viewDidLoad];
// Download JSON Feed
NSDictionary *feed = [self downloadFeed];
NSArray *streams = (NSArray *)[feed valueForKey:@"stream"];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 29.719023;
region.center.longitude = -114.157110;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
int Info;
for (Info = 0; Info < streams.count; Info++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:Info];
NSLog(@"Time: %@", [stream valueForKey:@"TheTime"]);
NSLog(@"Type: %@", [stream valueForKey:@"Type"]);
NSLog(@"Longitude: %@", [stream valueForKey:@"Longitude"]);
NSLog(@"Latitude: %@", [stream valueForKey:@"Latitude"]);
NSString *getLat = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Latitude"]];
NSString *getLong = [[NSString alloc] initWithFormat: @"%@", [stream valueForKey:@"Longitude"]];
NSString *getCoord = [[NSString alloc] initWithFormat: @"{%@,%@}", getLat, getLong];
getCoordinates = getCoord;
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"%@", [stream valueForKey:@"TheTime"];
ann.subtitle = @"%@", [stream valueForKey:@"Type"];
ann.coordinate = getCoordinates;
[mapView addAnnotation:ann];
}
}
Here is the code for DisplayMap
DisplayMap.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface DisplayMap : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@end
And now DisplayMap.m
#import "DisplayMap.h"
@implementation DisplayMap
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
-(void)dealloc{
[title release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取坐标是什么类型?不管怎样,它肯定没有被正确初始化。
假设您将纬度和经度作为字符串存储在字典中,这应该可以解决问题。
What type is getCoordinates? Whatever the case, its definitely not being initialized right.
Assuming that your storing Latitude and Longitude as strings in your dictionary, this should do the trick.
乍一看:
应该是
我猜
At first glance :
should be
I guess