2013-10-04 30 views
0

我正在構建基於MapKit的應用程序。我想創建一定數量的pin(比方說20),現在我用代碼指定了這些座標。例如,在我的執行文件中:iOS MapKit從外部文件創建Pin加載

#define PIN1_LATITUDE 43.504156 
#define PIN1_LONGITUDE 12.343405 

#define PIN2_LATITUDE 43.451696 
#define PIN2_LONGITUDE 12.488599 

等等所有20個值。問題是,我想根據某個參數加載和顯示不同的20個引腳組。所以我想將引腳的座標存儲在外部文件中,並在時間上加載1個座標。

它可能做到這一點?我應該使用哪種結構? 謝謝!

回答

0

爲引腳和其他數據創建自定義對象類。並將它們存儲在數組或字典中。隨時隨地使用。你也可以使用持久存儲。

.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]; 
+0

我不明白它..你能給我一些例子嗎? – carlodonz

+0

像這樣你可以創建。 – Vinodh

0
  • 創建用於20個引腳LAT &經度值
  • plist文件中或JSON文件
  • 您需要JSONKit lib或最好的ios框架類NSJSONSerialization如果你的應用定位的iOS5後來
  • 將文件添加到項目的資源文件夾
  • 編寫代碼,加載資源文件

這裏一個例子如何做到這一點使用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中是非常 在可可觸摸很好的支持。

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

記住要在後臺運行的線程或塊的NSOperation加載數據的代碼,你的應用程序可能會崩潰 - 踢出的跳板 - 採取太長加載。