Search code examples
objective-crestkit

How Can I use RestKit, in Objective C, to map latitude and longitude fields into a single CLLocationCoordinate2D?


I'm using RestKit to read from a REST API. Here is an excerpt of the JSON:

{"id":1,
"name":"Location 1",
"description":"Description for location 1",
"created_at":"2015-08-29T13:29:16.326Z",
"latitude":"23.1522671072",
"longitude":"-9.4321711561",
...

However, in my application the latitude & longitude are encapsulated within:

@property (nonatomic) CLLocationCoordinate2D coordinate;

What is the best way to transform the two latitude & longitude fields into a CLLocationCoordinate2D field?


Solution

  • This code snippet may help you.

    NSString *latStr = response[@"latitude"];
    NSString *longStr = response[@"latitude"];
    
    double latitude = [latStr doubleValue];
    double longitude = [longStr doubleValue];
    
    self.coordinate = CLLocationCoordinate2DMake(latitude, longitude);