2017-05-19 12 views
0

나는 모든 5 초 성공적으로 위도와 경도를 얻고, 나는 데이터베이스에 저장하고,하지만 난 매 5 초마다 30m 거리에 위치 데이터를 원하는거리와 시간을 기준으로 위도와 경도를 알고 싶습니다. <p>이</p>가의 ViewController 내 코드입니다 .... 내 프로젝트에서

당신이 NSPredicate를 생성하기위한 최소 및 최대 위도, 경도 값을 찾을해야하는 .m

//When I click button this method called... 

- (IBAction)getLocationDetails:(UIButton *)sender { 
[self CurrentLocationIdentifier]; 
timer = [NSTimer scheduledTimerWithTimeInterval:5.0f 
             target:self 
             selector:@selector(CurrentLocationIdentifier) 
             userInfo:nil 
             repeats:YES]; 

} 

-(void)CurrentLocationIdentifier { 

//---- For getting current gps location 
locationManager = [[CLLocationManager alloc]init]; 
locationManager.delegate = self; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

[locationManager startUpdatingLocation]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 

currentLocation = [locations objectAtIndex:0]; 
self.longitudeString = @(currentLocation.coordinate.longitude).stringValue; 
self.latitudeString = @(currentLocation.coordinate.latitude).stringValue;  
[locationManager stopUpdatingLocation]; 
manager.delegate = nil; 

NSLog(@"New longitude %@", self.longitudeString); 
NSLog(@"New latitude %@", self.latitudeString); 

dispatch_async(dispatch_get_main_queue(), ^{ 


CLGeocoder *geocoder = [[CLGeocoder alloc] init] ; 
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) 
{ 
    NSString *CountryArea; 

    if (!(error)) 
    { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     NSLog(@"\nCurrent Location Detected.......\n"); 
//    NSLog(@"placemark : %@",placemark); 
     NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "]; 
     NSString *address = [[NSString alloc]initWithString:locatedAt]; 
     NSLog(@"Address : %@", address); 
     self.addressString = [[NSString alloc]initWithString:address]; 

[self saveData]; 
} 
    else 
    { 
     NSLog(@"Geocode failed with error %@", error); 
     NSLog(@"\nCurrent Location Not Detected\n"); 
     //return; 
     CountryArea = NULL; 
    } 

}]; 


}); 

// Here is the distance calculation... 
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude]; 
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude]; 
CLLocationDistance distance = [startLocation distanceFromLocation:endLocation]; 

if (distance == 0) { 
    NSLog(@"Distance.... %f", distance); 
} 


} 


- (void)saveData { 

NSManagedObject * managedObj = [[NSManagedObject alloc]initWithEntity:self.GPSDatabaseED insertIntoManagedObjectContext:self.ad.managedObjectContext]; 

[managedObj setValue:self.longitudeString forKey:@"longitude"]; 
[managedObj setValue:self.latitudeString forKey:@"latitude"]; 
[managedObj setValue:self.addressString forKey:@"address"]; 
[managedObj setValue:self.UDIDString forKey:@"udid"]; 


NSError * errorObj; 

[self.ad.managedObjectContext save:&errorObj]; 


if (errorObj) { 

    NSLog(@"Something goes wrong"); 
}else 
{ 
    NSLog(@"Saved Successfully"); 
} 


} 

답변

2

여기에 가능한 솔루션입니다. 라디안

  1. 변환 학위

    -(float)deg2rad:(float)degrees{ 
         return degrees * M_PI/180; 
    } 
    
  2. KM의 최소 및 최대 위도, 경도 값 // 거리 값을 찾는 (30m)

    float searchDistance = 0.03; 
    
    float minLat = userLocation.coordinate.latitude - (searchDistance/69); 
    
    float maxLat = userLocation.coordinate.latitude + (searchDistance/69); 
    
    float minLon = userLocation.coordinate.latitude - searchDistance/fabs(cos([self deg2rad:userLocation.coordinate.latitude])*69); 
    
    float maxLon = userLocation.coordinate.longitude + searchDistance/fabs(cos([self deg2rad:userLocation.coordinate.latitude])*69); 
    
  3. 다음과 같이 술어를 작성하십시오.

    (210)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"latitude <= %f AND latitude >= %f AND longitude <= %f AND longitude >= %f", maxLat, minLat, maxLon, minLon]; 
    

userLocation 주위에 사각형을 만들 것입니다, 그것은 당신을 완벽하게 도움이 될 수 있습니다.

+0

오류 예상 ')' – Marking

+0

업데이트 답변, 지금 확인하십시오 .. – Lalji

+0

minLon 및 maxLon에 대한 계산 오류가 있습니다. 위도는 사용하지 말고 경도 만 사용하십시오. float minLon = userLocation.coordinate. 경도 - searchDistance/fabs (cos ([self deg2rad : userLocation.coordinate. 경도]) * 69); float maxLon = userLocation.coordinate.longitude + searchDistance/fabs (cos ([self deg2rad : userLocation.coordinate. 경도]) * 69); – Andrey