создание координат объектов из списка - PullRequest
2 голосов
/ 04 сентября 2011

Я пытаюсь загрузить аннотации из plist и отобразить на карте, но у меня возникают проблемы с:

а) динамическое присвоение координат и
б) отображение их на карте.

когда я физически назначаю широту и длину, как:

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

карта построена с аннотациями, но кажется, что все они расположены в одном месте?

Я так долго пытался решить это, есть ли лучший способ? Любая помощь будет оценена. я начинаю рассматривать их вручную как объекты.

вот мой список ...

Я хотел сделать два типа аннотаций, каждый из которых создавался как объект, а затем добавлялся в отдельный массив, чтобы я мог переключаться между ними в представлении карты.

вот мои аннотации.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;

вот аннотации.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>

Ответы [ 2 ]

1 голос
/ 04 сентября 2011

Код вроде бы в порядке.

Единственная проблема, которую я вижу, состоит в том, что координаты в листе находятся в обратном направлении.

Например, BB1 находится на широте -1, а долгота 53 - на Индийском океане.

Если парковочные места действительно находятся в Шеффилде, Великобритания, измените координаты в списке.

0 голосов
/ 04 сентября 2011

Я вижу ошибки в вашем коде.

1 вам нужно использовать массив для цикла for вот так

NSArray *array = [BlueBadgeContentsDict allKeys];

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

2 Вместо этого

tempCoordinate.longitude = [tempLat doubleValue];

вам нужно сделать это

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