2地点間の距離を取得したい。以下のコードを使用して、2 つの場所の緯度と経度を取得しています
-(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"];
}
}
そして、以下のコードを使用している2つの場所間の距離を取得する
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]];
8749107.212873 のような非常に大きな値が得られ、マイルに換算すると数千にもなりますが、2 つの場所はわずか 20 km しか離れていません。
コードに問題はありますか?