2011-09-04 75 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