JSON DoubleValue

Discussion in 'iOS Programming' started by jnoxx, Jun 14, 2012.

  1. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #1
    Hey guys,

    Having a really weird problem.
    We have an Taxi application which uses coordinates of existing locations and such. But really sometimes we have a small error (I didn't make the project, but I have to fix it :p), so basically, after some testing of what our client sended to us, the thing is, if you type in a street to google, you send those coordinates to google & save it, and when u need the details, you will send to google maps API again, so basically, when you get this BACK, it goes wrong..
    Google Maps API sends back a JSON, which contains latitude & longitude of a point, when you want to convert that to a CLLOcationCoordinate2D it goes wrong.

    Code:
    - (void) handleResponseForAddressSearch: (NSDictionary *)result
    {
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake([[result objectForKey:@"latitude"] doubleValue], [[result objectForKey:@"longitude"] doubleValue]);
        
        NSLog(@" result latitude & longitude: %@ & %@", [result objectForKey:@"latitude"], [result objectForKey:@"longitude"]);
        NSLog(@"latitude: %f & longitude: %f", location.latitude, location.longitude);
    As you can see here, i just posted a tiny bitsy of the code, so we get the result back from Google, and then try to get the DoubleValue of those 2 keys. Then I log them, the log basically says this.

    Code:
    2012-06-14 09:06:56.573 Dispatch! DEV[7840:707]  result latitude & longitude: 52.3358326 & 4.7787182
    2012-06-14 09:06:56.575 Dispatch! DEV[7840:707] latitude: 52.335833 & longitude: 4.778718
    
    As you can see, it's just a teensy bit different, which would be okay, but if you use these coordinates to get the street again from the API, it will give you a little bit different adress. like 2 houses further, but that could be just another street on a corner, and that causes confusion. So basically, i'm a bit stuck of how to solve it, write my own mapper?

    Greets, Thijs
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Not sure but there may be small conversion errors. Make sure your doubles don't get turned into floats. There might also be slight errors introduced by NSLog. You print one with %@ and the other by %f. Try doing them both the same way. Print out the doubleValue. Try using %g or look up these printf operators to make sure you're using the right one. There may be no differences at all, just differences in the precision that they are printed.
     
  3. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #3
    But if I send them back to the Google Maps API, it will give me the wrong adress, sometimes only off by 10 meter, because of the precision of the coordinates, and that's whats bugging me ^_-
    So I send the CLLocationCoordinate back to the webservice. And that's where it goes wrong.
    I'll try to figure it out more :)
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    How do you send it back? That's probably another conversion between double and string. Each conversion is a place for slight changes, rounding errors, etc.

    Also, if you download it as a string and then later upload it as a string then you don't need to any conversions. Just send back what you received.
     
  5. jnoxx, Jun 15, 2012
    Last edited: Jun 15, 2012

    jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    Hey Phoney,

    The thing is, first time you save it to a CLLocation, and than you save that to a small database, once you need it agian, you send those coordinates to google to get the adress again, and that's where we noticed it went wrong..
    U send the CLLocation back to google, and then it gives you back the wrong adress because of the rounded double.
    Therefore, there is only this one "step" between thse 2 code calls.
    I understand I could send back the string, but the implementation is curently that you have the CLLocation so you can put in the pin on a map, and then later can retrieve i. So I kinda need it to work like this >.< So confused..
    Thanks for responding though.

    EDIT: after a bit of debugging and going through hell, I think I figured it out, but it's just a major bummer.
    Is it possible that the float maximum supports 6 characters after the comma? The Google API is sending 7 chars after comma/point, whatever u use. So therefore it's just shortening it, and kinda ruining what we're doing..
    If anyone has an idea, would be great.

    EDIT 2: I got it fixed.. The thing is, NSLog doesn't support those 7 digit's with the %f.. I have tried everything Phoney said, and what I found on the internet, so I tried adding hardcoded that it had to show me 7 digits. And right of the bat, that worked, so basically, when I did it, I had to do %0.7lf (long float), and this gave me the right things back in the NSLog, and when I send it back to google, I had to do %0.7lf too instead of %lf (which I did before), and now it all works, frikking great :p wasted nearly 12 hours with several thorough iOS developers..
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Yes, floating point numbers can be odd. They are not the same as integers in various ways. I think I never heard of long float. Only long double.
     
  7. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    Well, I hope someone who has this issue ever finds this post, and it helps them out, and maybe you won't forget it too and code it right from the bat!
     

Share This Page