2016-08-12 5 views
0

私は以下のように2つの位置距離を計算していました。結果は常に0メートルです。 誰かを助けることができますか?場所の距離は常に結果0を返します - ios sdk

double distance = [self.currentLocation distanceFromLocation:location1]; 
NSLog(@"Distance i meters: %.8f", distance); 
if(distance <30) 
{ 
    NSLog(@"Yeah, this place is inside my circle"); 
} 
else 
{ 
    NSLog(@"Oops!! its too far"); 
} 

おかげ

+0

スタート'self.currentLocation'と' location1'は両方とも非nilです。いずれかが 'nil 'の場合、' distance'は '0'になります。 – rmaddy

+0

あなたは私の答え@ user831098があなたに役立つのを見ましたか? –

答えて

0

私は以下のようにコーディングして正しい結果が得られますことを確認することによって

CLLocation *location1 = [[CLLocation alloc] initWithLatitude:DEFAULT_LAT longitude:DEFAULT_LNG]; 

    CLLocationCoordinate2D annocoord = CLLocationCoordinate2DMake(location1.coordinate.latitude, location1.coordinate.longitude); 
    CLLocationCoordinate2D usercoord = self.locationManager.location.coordinate; 

    CLLocation *loc = [[CLLocation alloc] initWithLatitude:annocoord.latitude longitude:annocoord.longitude]; 
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.locationManager.location.coordinate.latitude longitude:self.locationManager.location.coordinate.longitude]; 

    NSLog(@"LOC = %f, %f", loc.coordinate.latitude, loc.coordinate.longitude); 
    NSLog(@"LOC2 = %f, %f", loc2.coordinate.latitude, loc2.coordinate.longitude); 

    CLLocationDistance dist = [loc distanceFromLocation:loc2]; 

    NSLog(@"DIST: %f", dist); // Wrong formatting may show wrong value! 
0

このコードを試してみてください:使用CLLocationDistance GET距離について:

CLLocationDistance distance = [self.currentLocation distanceFromLocation:location1]; 
float TotalMile=distance/1609.344; 
関連する問題