如何存储坐标 - iOS

时间:2013-09-11 10:17:26

标签: ios arrays cocoa-touch mapkit coordinates

我有一个iOS应用程序,可以从JSON提要中解析一些数据。下载数据之一是一组坐标。我试图获取这些坐标并在iOS中的MapView上显示它们。但是我收到以下错误:

  

初始化' cllocationcoordinate2d'表达不兼容的类型' id'

这是我的代码:

    int choose = 4;
    NSArray *location_lat = [[[res objectForKey:@"data"] valueForKey:@"location"] valueForKey:@"latitude"];
    NSArray *location_long = [[[res objectForKey:@"data"] valueForKey:@"location"] valueForKey:@"longitude"];

    NSLog(@"\n\nLat:%f Lon:%f", [location_lat[choose] doubleValue], [location_long[choose] doubleValue]);

    CLLocationCoordinate2D lat = [location_lat[choose] doubleValue];
    CLLocationCoordinate2D lon = [location_long[choose] doubleValue];

    MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
    region.center.latitude = lat;
    region.center.longitude = lon;
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [map setRegion:region animated: YES];

    ContentView *ann = [[ContentView alloc] init];
    ann.title = @"Current Location";
    ann.subtitle = @"";
    ann.coordinate = region.center;
    [map addAnnotation:ann];

我不明白我做错了什么......请帮助。

谢谢,Dan。

2 个答案:

答案 0 :(得分:1)

纬度和经度似乎不是CLLocationCoordinate2D类型,它们看起来像是double。试试这个:

double lat = [location_lat[choose] doubleValue];
double lon = [location_long[choose] doubleValue];

CLLocationCoordinate2D代表纬度和经度,因此您也可以这样做:

CLLocationCoordinate2D center = CLLocationCoordinate2DMake([location_lat[choose] doubleValue], location_long[choose] doubleValue]);

MKCoordinateRegion region = { {0.0, 0.0}, {0.0, 0.0} };
region.center = center;

答案 1 :(得分:0)

使用此宏:

 CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(x, y);