2011-09-04 71 views
2

我試圖從一個plist中加載的註釋,並顯示到地圖,但具有麻煩座標的對象:創建從一個plist中

一)分配的座標動態,並
b)中的地圖上顯示它們。

當我物理分配lat和長像:

tempCoordinate.latitude = 53.381164; 
tempCoordinate.longitude = -1.471798; 

地圖繪製與註釋,但它似乎繪製他們都在同一個地方?

我一直在努力工作這麼久,有沒有更好的方法?任何幫助將不勝感激。我開始考慮手動將它們全部輸入爲對象。

這是我的plist ....

我想使2種類型的註釋的每個所創建的作爲對象,然後加入到單獨的陣列,這樣我可以在地圖視圖在它們之間切換。

這裏是我的annotations.h類:

@property (copy) NSString *streetName; 
@property (copy) NSString *bays; 
@property (copy) NSString *type; 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

- (id)initWithAnnotationName:(NSString *)newStreetName    
        theBays:(NSString *)newBays     
        theType:(NSString *)newType 
       theCoordinate:(CLLocationCoordinate2D)newCoordinate; 

這裏是annotations.m:

if ((self = [super init])) {  

     type = [newType copy]; 
     streetName = [newStreetName copy];   
     bays = [newBays copy]; 
     coordinate = newCoordinate;   
    } 

這裏是mapView.h

parkingArray1 = [[NSMutableArray alloc] init]; 
//NSMutableArray *testArr = [[NSMutableArray alloc] init]; 

//path of plist 
NSString *fullPathToPList=[[NSBundle mainBundle] pathForResource:@"AnnotationList" ofType:@"plist"]; 

//temp values for dictionary enumeration and creating annotation object 
NSDictionary *plistDict, *BlueBadgeDict, *BlueBadgeContentsDict; 

plistDict = [NSDictionary dictionaryWithContentsOfFile: fullPathToPList]; 

//enumerate through BlueBadge dictionary 
BlueBadgeDict = [plistDict valueForKey: @"BlueBadge"]; 

for (NSString *firstLevelString in BlueBadgeDict){ 

    BlueBadgeContentsDict = [BlueBadgeDict valueForKey:firstLevelString]; 

    NSString *tempStreetName, *tempBays, *tempType, *tempLat, *tempLong; 
    CLLocationCoordinate2D tempCoordinate; 

    for (NSString *secondLevelString in BlueBadgeContentsDict){ 

     //assign temp values from dict to string 
     if ([secondLevelString isEqualToString:@"StreetName"]) 
      tempStreetName = [BlueBadgeContentsDict valueForKey:secondLevelString]; 
     else if ([secondLevelString isEqualToString:@"Bays"]) 
      tempBays = [BlueBadgeContentsDict valueForKey:secondLevelString]; 
     else if ([secondLevelString isEqualToString:@"Longitude"]) 
      tempLong = [BlueBadgeContentsDict valueForKey:secondLevelString]; 
     else if ([secondLevelString isEqualToString:@"Latitude"]) 
      tempLat = [BlueBadgeContentsDict valueForKey:secondLevelString];    
    } 

    //pass plist root value 
    tempType = firstLevelString; 

    tempCoordinate.latitude = [tempLat doubleValue]; 
    tempCoordinate.longitude = [tempLong doubleValue]; 

    //create object 
    Annotation *tempAnnotation = [[Annotation alloc] initWithAnnotationName:tempStreetName theBays:tempBays theType:tempType theCoordinate:tempCoordinate]; 

    //add the object to the mutable array 
    [parkingArray1 addObject:tempAnnotation]; 

    [tempAnnotation release];  

} 

//display array on map  
[self.mapView addAnnotations:parkingArray1]; 

這裏是XML代碼

<dict> 
    <key>BlueBadge</key> 
    <dict> 
    <key>BB1</key> 
    <dict> 
     <key>Bays</key> 
     <string>4</string>   
     <key>Latitude</key> 
     <string>-1.466822</string> 
     <key>Longitude</key> 
     <string>53.37725</string>   
     <key>StreetName</key> 
     <string>Arundel Lane</string> 
    </dict> 
    <key>BB2</key> 
    <dict> 
     <key>Bays</key> 
     <string>5</string>   
     <key>Latitude</key> 
     <string>-1.471994</string> 
     <key>Longitude</key> 
     <string>53.381071</string>   
     <key>StreetName</key> 
     <string>Balm Green</string> 
    </dict> 
    <key>BB3</key> 
    <dict> 
     <key>Bays</key> 
     <string>1</string>   
     <key>Latitude</key> 
     <string>-1.466739</string> 
     <key>Longitude</key> 
     <string>53.374164</string>   
     <key>StreetName</key> 
     <string>Brittain Street</string> 
    </dict> 
</dict> 
</dict> 
+1

請將plist作爲xml(如果很長,然後是一個小樣本)發佈並將其格式化爲「代碼」。 – Anna

回答

1

代碼似乎好的。

我看到的唯一問題是plist中的座標是向後的。

例如,BB1位於印度洋的緯度-1和經度53。

如果停車場實際上位於英國謝菲爾德,請翻轉plist中的座標。

+0

哦不!全校男生。我一整天都在主演! 感謝您的發現(地理從來就不是一個強項) – ChrisM

0

我可以看到你的代碼中的錯誤。

1,你需要在相反使用數組for循環這樣

NSArray *array = [BlueBadgeContentsDict allKeys]; 

for(NSString *secondLevelString in array) 
{ 
    ..... 
} 

2的這個

tempCoordinate.longitude = [tempLat doubleValue]; 

你需要這樣做

tempCoordinate.longitude = [tempLong doubleValue]; 
+0

嗨,[tempLong doubleValue]只是一個錯字,而我正在複製到網站,我現在已經改變它。 至於陣列我需要什麼?我有這個數組添加對象 [parkingArray1 addObject:tempAnnotation]; 這不夠嗎? – ChrisM