skip to Main Content

Sorry if this is a very noobish question, I just could not find any answer anywhere online. I am trying to encode GPS coordinates to JSON before sending with a Facebook Graph API. I think I am doing the right thing, but keep getting an error back from the API (which I am pretty sure relates to how I am creating this structure).

Is this the right way to encode GPS coordinates in Objective C?

NSMutableDictionary *params1 = [NSMutableDictionary dictionaryWithCapacity:3L];

NSString *latStr = [NSString stringWithFormat:@"%f", Coord.latitude];
NSString *lngStr = [NSString stringWithFormat:@"%f", Coord.longitude];
NSString *coordinatesString = [[NSString alloc] initWithFormat:@"%@,%@", latStr, lngStr];

[params1 setObject:dictionaryCoordinates forKey:@"coordinates"];

PS: Coord is a CLLocationCoordinate2D

This is what I am getting, which seems right to me:

coordinates =     {
    latitude = "33.24234";
    longitude = "-120.34224";
};

Thanks!!!

2

Answers


  1. I have no experience with the API, but I do not think, that coordinates are strings. But you send them as strings.

    Use instances of NSNumber instead of NSString. And you should have a view toNSJSONParser`.

    Login or Signup to reply.
  2. If you need actual numbers then you can do:

    NSDictionary *coordinates = @{ @"coordinates": @{ @"latitude": @(Coord.latitude), @"longitude": @(Coord.longitude) } };
    

    The @(...) wraps the number as an NSNumber.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search