views:

49

answers:

1

I am trying to use an http request to google maps to obtain the driving distance between two locations using JSON. However, it seems these are treated different in a browser than in the (iphone) app.

I create an NSString which holds the URL, using coordinates and %20 (a space). I NSLog() the URL to make sure, and it seems ok (ie it works fine in browser and looks fine)...but when NSLogging the string initialised with the contents of that URL, I get (null).

Here is the code:

NSString *urlString=[[NSString alloc] initWithFormat:@"http://maps.google.com/maps/nav?q=from:%.7f%@%.7f%@to:%.7f%@%.7f", testLocation.coordinate.latitude, @"%%20", testLocation.coordinate.longitude, @"%%20", thePark.coordinate.latitude, @"%%20", thePark.coordinate.longitude];

which I then NSLog() and get http://maps.google.com/maps/nav?q=from:51.4986110%20-0.1236110%20to:51.4960938%20-0.2200041 ...to no avail.

NSString *json=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]];

When I NSLog() this, it prints null. Does anyone have any suggestions as to why this might be happening or an easier way to do it? I plan to then parse the JSON and get the drving distance

+1  A: 

You should escape the urlString using "stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding" instead of escaping manually with %20.


Edit:

I tested with these 3 lines of codes and I got the json string.

NSString *urlString=[[[[NSString alloc] initWithFormat:@"http://maps.google.com/maps/nav?q=from:%.7f %.7f to:%.7f %.7f", 51.4986110, -0.1236110, 51.4960938, -0.2200041] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] autorelease];

NSString *json=[[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
NSLog(@"json = %@", json);
tmin
I now get EXC_BAD_ACCESS when initialising a new string from the contents of that URL. NSLogging the URL now comes up as: http://maps.google.com/maps/nav?q=from:51.4986110220to:51.49609382
Felixs
If I break it up into two lines: NSString *url=[[NSString alloc] initWithFormat:@"http://maps.google.com/maps/nav?q=from:%.7f %.7f to:%.7f %.7f", 51.4986110, -0.1236110, 51.4960938, -0.2200041]; NSString *url2=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];NSLog first URL: http://maps.google.com/maps/nav?q=from:51.4986110 -0.1236110 to:51.4960938 -0.2200041NSLog second URL: http://maps.google.com/maps/nav?q=from:51.4986110220to:51.49609382which then gives "The operation couldn’t be completed. (Cocoa error 256.)" although it doesn't crash
Felixs
Please see the edit.
tmin
Thanks - It's working perfectly now. One Question though: why do they have to be autoreleased? Surely using alloc is enough?
Felixs
autorelease is used so that the NSString objects will get released from memory at the end of runloop. You definitely need to know Cocoa Memory Management to program on iPhone. Here is the link to the rules. http://developer.apple.com/iphone/library/documentation/cocoa/conceptual/memorymgmt/Articles/mmRules.html
tmin