使用 NSDictionary 在地图上添加注释?

发布于 2024-09-29 20:58:22 字数 2457 浏览 3 评论 0原文

感谢您提前提供的帮助...在过去的几个小时里,这个帮助一直困扰着我。

我目前正在提取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-10-06 20:58:22

获取坐标是什么类型?不管怎样,它肯定没有被正确初始化。

假设您将纬度和经度作为字符串存储在字典中,这应该可以解决问题。

double lat = [[stream valueForKey:@"Latitude"] doubleValue];
double lon = [[stream valueForKey:@"Longitude"] doubleValue];
CLLocationCoordinate2D coord = { lat, lon };

DisplayMap *ann = [[DisplayMap alloc] init]; 
ann.title = [stream valueForKey:@"TheTime"];
ann.subtitle = [stream valueForKey:@"Type"]; 
ann.coordinate = coord;

[mapView addAnnotation:ann];

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.

double lat = [[stream valueForKey:@"Latitude"] doubleValue];
double lon = [[stream valueForKey:@"Longitude"] doubleValue];
CLLocationCoordinate2D coord = { lat, lon };

DisplayMap *ann = [[DisplayMap alloc] init]; 
ann.title = [stream valueForKey:@"TheTime"];
ann.subtitle = [stream valueForKey:@"Type"]; 
ann.coordinate = coord;

[mapView addAnnotation:ann];
南街女流氓 2024-10-06 20:58:22

乍一看:

  ann.title = @"%@", [stream valueForKey:@"TheTime"];

应该是

  ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"TheTime"]];

我猜

At first glance :

  ann.title = @"%@", [stream valueForKey:@"TheTime"];

should be

  ann.title = [NSString stringWithFormat: @"%@", [stream valueForKey:@"TheTime"]];

I guess

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文