0

I wants to get distance between two locations. I am getting latitude and longitude of two locations using below code

-(void)getLatLongOfLoac:(NSString *)locStr{
    locStr = [locStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *geocodeUrl = [NSString stringWithFormat:@"%@://maps.google.com/maps/api/geocode/json?address=%@&sensor=false", 1 ? @"http" : @"https", locStr];

    ASIFormDataRequest *request   =  [ASIFormDataRequest requestWithURL:[NSURL URLWithString:geocodeUrl]];
    [request setRequestMethod:@"GET"];
    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [request setDelegate:self];
    [request startAsynchronous];

}

-(void)requestDone:(ASIHTTPRequest *)request{
    NSString *responseString = [request responseString];
    SBJsonParser *json          = [[SBJsonParser alloc] init];
    NSError *jsonError          = nil;
    NSDictionary *parsedJSON    = [json objectWithString:responseString error:&jsonError];

   if(self.locationTableView.tag == 0){
      self.latlongForPicUp = [[[[parsedJSON valueForKey:@"results"] objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"];
   }
   else{
       self.latlongForDropOff = [[[[parsedJSON valueForKey:@"results"] objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"];
   }

}

and to get distance between two location i am using below code

CLLocation *locA = [[CLLocation alloc] initWithLatitude:[[self.latlongForDropOff valueForKey:@"lat"] floatValue]longitude:[[self.latlongForDropOff valueForKey:@"lng"] floatValue]];

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[self.latlongForPicUp valueForKey:@"lat"] floatValue] longitude:[[self.latlongForPicUp valueForKey:@"lng"]floatValue ]];

   // CLLocationDistance distance = [locA distanceFromLocation:locB];
//float distInMile = 0.000621371192 * [locA distanceFromLocation:locB];
NSString *distance = [ NSString stringWithFormat:@"%f",[locA distanceFromLocation:locB]];

Its giving me a very large value like 8749107.212873 and after converting into its mile even its comes out to be some thousand but the two locations are just 20 km away.

Is there any problem in code?

287986
  • 93
  • 1
  • 12
  • 2
    Did you noticed you are setting [[self.latlongForPicUp valueForKey:@"lng"] floatValue] for latitude and [[self.latlongForPicUp valueForKey:@"lat"]floatValue ]];? Wouldn't be [[self.latlongForPicUp valueForKey:@"lat"] floatValue] for latitude and [[self.latlongForPicUp valueForKey:@"lng"] floatValue] for longitude? – scollaco Nov 12 '13 at 11:34
  • even its giving wrong result – 287986 Nov 12 '13 at 11:39
  • for latitude and longitude, I always use double instead of float, because it's more accurate. In my opinion, you should try it and see what happens. – scollaco Nov 12 '13 at 11:48
  • NSLog the coordinates of locA and locB to be sure they are what you think they are. –  Nov 12 '13 at 12:23
  • @scollaco You are absolutely right. Thanks. Please post ur answer i would like to mark it right answer.. But there is some problem , first time it gives correct result but when again i used same location then with "Double" its again giving wrong values – 287986 Nov 12 '13 at 12:56
  • As Anna Karenina said, you should NSLog to see what you are, in fact getting. :) – scollaco Nov 12 '13 at 13:17

2 Answers2

4

You are setting the latitude and longitude wrong when creating the second CCLocation:

You are doing:

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[self.latlongForPicUp valueForKey:@"lng"] floatValue] longitude:[[self.latlongForPicUp valueForKey:@"lat"]floatValue ]];

You should do:

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[self.latlongForPicUp valueForKey:@"lat"] floatValue] longitude:[[self.latlongForPicUp valueForKey:@"lng"]floatValue ]];

You are swapping the values.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • @287986, for latitude and longitude, I always use double instead of float, because it's more accurate. In my opinion, you should try it and see what happens. – scollaco Nov 12 '13 at 11:43
  • check that the latitude and longitude are correct, print the value of locA and locB and see them in a map – Antonio MG Nov 13 '13 at 10:39
0

Did you noticed you are setting

[[self.latlongForPicUp valueForKey:@"lng"] floatValue] for latitude and [[self.latlongForPicUp valueForKey:@"lat"]floatValue ]];? 

Wouldn't be

[[self.latlongForPicUp valueForKey:@"lat"] floatValue] for latitude and [[self.latlongForPicUp valueForKey:@"lng"] floatValue] for longitude?

And for latitude and longitude, I suggest to use double instead of float, because it's more accurate. In my opinion, you should try it and see what happens.

scollaco
  • 947
  • 8
  • 13