【问题标题】:iOS MapKit creating Pin loading from external fileiOS MapKit 从外部文件创建 Pin 加载
【发布时间】:2013-10-04 11:53:25
【问题描述】:

我正在构建一个基于 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组。

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

【问题讨论】:

    标签: ios mapkit coordinates mkannotation mkpinannotationview


    【解决方案1】:

    为引脚和其他数据创建自定义对象类。并将它们存储在 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
    

    viewcontroller.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];
    

    【讨论】:

    • 我不明白..你能给我一些例子吗?
    • 这样你可以创建。
    【解决方案2】:
    • 为 20 针 lat & lng 值创建 PLIST 文件或 json 文件
    • 如果您的应用面向 iOS5 及更高版本,您将需要 JSONKit lib 或最好是 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 块中运行加载数据代码,您的应用可能会崩溃 - 被跳板踢出 - 因为加载时间过长。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多