0

我正在构建一个基于 MapKit 的应用程序。我想创建一定数量的引脚(比如说 20 个),现在我通过代码指定这些坐标。例如,在我的实现文件中:

#define PIN1_LATITUDE 43.504156 
#define PIN1_LONGITUDE 12.343405

#define PIN2_LATITUDE 43.451696
#define PIN2_LONGITUDE 12.488599

对于所有 20 个值,依此类推。问题是,我想根据某个参数加载和显示不同的 20 个引脚组。所以我想将pin的坐标存储在外部文件上并一次加载1组。

有可能这样做吗?我应该使用哪种结构?谢谢!

4

2 回答 2

0

为引脚和其他数据创建自定义对象类。并将它们存储在 Array 或 dictionary 中。并在需要时使用。您也可以使用持久存储。

。H

@interface MapAnnotation : NSObject 
@property(nonatomic, assign)    double latitude;
@property(nonatomic, assign)    double longitude;
@property (nonatomic)           int Id;
@end

.m

 @interface MapAnnotation 
      @synthesize latitude,longitude,Id;
    @end

视图控制器.m

 NSMutableArray *annotationarray = [NSMutableArray alloc]init];

 MapAnnotation *newAnnoataion = [[MapAnnotation allo]init];
 newAnnoataion.latitude = 49.05678;
 newAnnoataion.longitude = 69.05678;
 newAnnoataion.id = 1;
[annotationarray addObject newAnnoataion];
于 2013-10-04T12:19:13.653 回答
0
  • 为 20 针 lat & lng 值创建一个 PLIST 文件或 json 文件
  • 如果您的应用程序针对 iOS5 及更高版本,您将需要JSONKit 库或最好是 ios 框架类NSJSONSerialization
  • 将文件添加到项目资源文件夹
  • 编写将加载资源文件的代码

这是一个如何使用 JSONKit 做到这一点的示例

创建一个 json 文件,将以下 json 示例复制粘贴到poi_data.json中,加载 json 文件。

    {
        "poi": [
            {
                "id": "1",
                "lat": "43.668997",
                "lng": "-79.385093"
            },
            {
                "id": "1",
                "lat": "43.668997",
                "lng": "-79.385093"
            },
            {
                "id": "1",
                "lat": "43.668997",
                "lng": "-79.385093"
            }
        ]
    }



    - (void)loadDataFromFile {
        NSString* path = [[NSBundle mainBundle] pathForResource:@"poi_data"
                                                         ofType:@"json"];
        NSString* content = [NSString stringWithContentsOfFile:path
                                                      encoding:NSUTF8StringEncoding
                                                         error:NULL];

        NSDictionary *poiCollection = [content objectFromJSONStringWithParseOptions:JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode error:nil];



            NSArray* pois = [result objectForKey:@"poi"];
                for (NSInteger i = 0; i < [pois count]; i++) {

                    NSDictionary* node = (NSDictionary*)[poi objectAtIndex:i];

                    CGFloat lat = [[node objectForKey:@"lat"] doubleValue];
                    CGFloat lng = [[node objectForKey:@"lng"] doubleValue];
            } 
}
  • 你也可以使用 PLIST 并加载到一个 NSDictionary,plist 在 Cocoa Touch 中得到了很好的支持。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/PropertyLists/QuickStartPlist/QuickStartPlist.html#//apple_ref/doc/uid/10000048i-CH4-SW5

请记住在后台线程或 NSOperation 块中运行加载数据代码,您的应用程序可能会崩溃 - 被跳板踢出 - 因为加载时间过长。

于 2013-10-04T15:35:03.517 回答