1

I have an iOS application which parses some data from a JSON feed. One of the bit of downloaded data is a set of coordinates. I am trying to get those coordinates and display them on a MapView in iOS. But I get the following error:

initializing 'cllocationcoordinate2d' with an expression of incompatible type 'id'

Here is my code:

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

I don't understand what I am doing wrong... Please help.

Thanks, Dan.

4

2 回答 2

1

纬度和经度似乎不是 type CLLocationCoordinate2D,看起来它们是doubles。尝试这个:

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;
于 2013-09-11T10:21:40.703 回答
0

使用这个宏:

 CLLocationCoordinate2D cord = CLLocationCoordinate2DMake(x, y);
于 2013-09-11T10:21:50.960 回答