質問

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?

役に立ちましたか?

解決 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?

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top