2017-05-19 28 views
0

在我的項目中,我每5秒成功獲取經度和緯度,並且我保存在數據庫中,但是我希望每5秒和每30米距離的位置數據......我想根據距離和時間獲取位置的經緯度?

這是我在viewController .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

這裏是一個可能的解決方案:

你需要找到最小和最大的經緯度值創建NSPredicate

  1. 轉換爲弧度

    -(float)deg2rad:(float)degrees{ 
         return degrees * M_PI/180; 
    } 
    
  2. 找到在KM的最小和最大經緯度值//距離值(30米)

    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. 創建謂詞如下

    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。longitude])* 69); float maxLon = userLocation.coordinate.longitude + searchDistance/fabs(cos([self deg2rad:userLocation.coordinate。longitude])* 69); – Andrey

相關問題