开发者

Longitude latitude from plist to CLLocation

开发者 https://www.devze.com 2023-04-11 09:24 出处:网络
I have the following plist: <plist version=\"1.0\"> <array> <dict> <key>description</key>

I have the following plist:

    <plist version="1.0">
<array>
    <dict>
        <key>description</key>
        <string>Dodge Viper</string>
        <k开发者_StackOverflow社区ey>title</key>
        <string>Bil</string>
        <key>longitude</key>
        <real>19.027054</real>
        <key>latitude</key>
        <real>36.129471</real>
        <key>show</key>
        <true/>
    </dict>
    <dict>
        <key>description</key>
        <string>Dodge RAM</string>
        <key>title</key>
        <string>Truck</string>
        <key>longitude</key>
        <real>1.026062</real>
        <key>latitude</key>
        <real>46.128918</real>
        <key>show</key>
        <true/>
    </dict>
    <dict>
        <key>description</key>
        <string>Tøf tøf</string>
        <key>title</key>
        <string>Lastbil</string>
        <key>longitude</key>
        <integer>0</integer>
        <key>latitude</key>
        <real>56.128918</real>
        <key>show</key>
        <true/>
    </dict>
    <dict>
        <key>description</key>
        <string>Black Pearl</string>
        <key>title</key>
        <string>Piratskib</string>
        <key>longitude</key>
        <integer>0</integer>
        <key>latitude</key>
        <real>25.128918</real>
        <key>show</key>
        <false/>
    </dict>
</array>
</plist>

And I have a basic mapview controller.

What I would like is to create annotations for each item using the longitude and altitude key.

Problem is, when I try I get problems with "double" -> I don't know how to read my value from the plist as a double, I have tried converting it, but it doesn't seem to work.

My code in the map view controller is like this:

Currently there's a problem at "double latitude2 = [str objectForKey:@"latitude"];" but I included the line anyway to show my intentions.

-(void)viewDidLoad {
    [super viewDidLoad];



    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"enheder" ofType:@"plist"];

    NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for (NSString *str in array2) {

        double latitude2 = [str objectForKey:@"latitude"];


    [mapview setMapType:MKMapTypeHybrid];
    [mapview setZoomEnabled:YES];
    [mapview setScrollEnabled:YES];

    MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
    region.center.latitude = latitude2;
    region.center.longitude = 9.027044;
    region.span.longitudeDelta = 0.001f;
    region.span.latitudeDelta = 0.001f;
    [mapview setRegion:region animated:YES];

    NewClass *ann = [[NewClass alloc] init];
    ann.title = @"Title";
    ann.subtitle = @"Sub";
    ann.coordinate = region.center;
    [mapview addAnnotation:ann];

   }  




}

I am very much a novice when it comes to programming for iPhone, so it's very likely I've missed something fundamental. But any help would be much appreciated.


Your plist han an array of dictionary not an array of strings. change your code in this way

for (NSDictionary *dict in array2) 
    double latitude2 = [[dict objectForKey:@"latitude"] doubleValue];


for (NSString *str in array2) { 

in this statement NSString replace with NSDictionary i think it will work

0

精彩评论

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

关注公众号