0

I am trying to build an app with a map in which the user would select his origin address and destination address.. It all works fine, but I can't access the Google API Distance Matrix..

I am try with following:

NSString *urlPath = [NSString stringWithFormat:@"/maps/api/distancematrix/xml?origins=%@=%@&mode=driving&language=en-EN&sensor=false" ,polazisteField.text , odredisteField.text];
NSURL *url = [[NSURL alloc]initWithScheme:@"http" host:@"maps.googleapis.com" path:urlPath];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init]autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLResponse *response ;
NSError *error;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
address.text = result;

, but have no luck, any idea how to resolve this?

hbk
  • 10,908
  • 11
  • 91
  • 124
dsafa
  • 783
  • 2
  • 8
  • 29
  • 1
    you can use this also for finding distance b/w two location:// get CLLocation fot both addresses CLLocation *location = [[CLLocation alloc] initWithLatitude:address.latitude longitude:address.longitude]; // calculate distance between them CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation]; – Mudit Bajpai Mar 19 '12 at 07:00
  • i tried with this but the [firstLocation distanceFromLocation : secondLocation] is based od air distance, not road.. – dsafa Mar 19 '12 at 10:12

1 Answers1

0

I think your call has been considred as invalid API call.

try below code that is edited from your code snippet.

 NSString *urlPath = [NSString stringWithFormat:@"/maps/api/distancematrix/json?origins=%@&destinations=%@&mode=driving&language=en-EN&sensor=false" ,polazisteField.text , odredisteField.text];
NSURL *url = [[NSURL alloc]initWithScheme:@"http" host:@"maps.googleapis.com" path:urlPath];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLResponse *response ;
NSError *error;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSMutableDictionary *jsonDict= (NSMutableDictionary*)[NSJSONSerialization  JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableDictionary *newdict=[jsonDict valueForKey:@"rows"];
NSArray *elementsArr=[newdict valueForKey:@"elements"];
NSArray *arr=[elementsArr objectAtIndex:0];
NSDictionary *dict=[arr objectAtIndex:0];
NSMutableDictionary *distanceDict=[dict valueForKey:@"distance"];
NSLog(@"distance:%@",[distanceDict valueForKey:@"text"]);

   NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
   address.text = [distanceDict valueForKey:@"text"];
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
  • what is the difference between my code and yours ? what did you changed ? – dsafa Mar 19 '12 at 10:13
  • Has it worked for you?? Let me know than I will tell you the difference between your and mine code. – Kuldeep Mar 19 '12 at 10:14
  • Compare this line to your : @"/maps/api/distancematrix/xml?origins=%@& destinations=%@&mode=driving&language=en-EN&sensor=false" – Kuldeep Mar 19 '12 at 10:29
  • it's not working.. just for testing i created a label to preview the results.. it didn't return the distance, only "......where could the problem be ? in the API address or ? – dsafa Mar 19 '12 at 11:57
  • Dear the API will not give you direct what you need. You will need to parse that xml. You can see the response (distance) by this link which we have included in the code. http://maps.googleapis.com/maps/api/distancematrix/xml?origins=cupertino&destinations=infinite%20loop&mode=driving&language=en-EN&sensor=false – Kuldeep Mar 19 '12 at 12:05
  • Use the above edited snippet in your code will solve your problem will have distance in the "address.text" directly. – Kuldeep Mar 19 '12 at 12:28
  • it doesn't work...i used your code but nothing.. the label changes it's value, but it stays empty... i even copied your link but nothing... – dsafa Mar 19 '12 at 12:53
  • Now use the snippet. I have edited little bit will give you distance definately. Earlier one parameter was wrong that I have edited. – Kuldeep Mar 19 '12 at 12:59
  • ooo...it works now.. thanks a lot.. where was the problem ? thanks again for your time and effort – dsafa Mar 19 '12 at 13:04