开发者

creating coordinates objects from a plist

I am trying to load annotations from a plist and display onto a map, but having trouble with:

a) assigning the coordinates dynamically, and

b) displaying them on a map.

when i physically assign the lat and long like:

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

the map is plotted with annotations but it seems to plot them all in the same place?

i have been trying to work this out for so long, is there a better way? any help will be greatly appreciated. i am starting to consider manually typing them all in as objects.

here is my plist....

i wanted to make 2 types of annotations each created as object then added to separate array so that i can switch between them on the map view.

here is my annotations.h class:

@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;

here is the annotations.m:

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

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

here is the 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];

//enum开发者_如何学Cerate 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];

here is the xml code

<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>


The code seems to be ok.

The only problem I see is that the coordinates in the plist are backwards.

For example, BB1 is at latitude -1 and longitude 53 which is somewhere in the Indian Ocean.

If the parking lots are actually in Sheffield, UK, flip the coordinates in the plist.


I can see to mistakes in your code.

1 you need to use an array in for loop like this

NSArray *array = [BlueBadgeContentsDict allKeys];

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

2 Instead of this

tempCoordinate.longitude = [tempLat doubleValue];

you need to do this

tempCoordinate.longitude = [tempLong doubleValue];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜