gpt4 book ai didi

ios - 我想根据距离和时间获取位置经纬度?

转载 作者:行者123 更新时间:2023-11-28 21:09:43 25 4
gpt4 key购买 nike

在我的项目中,我每 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");
}


}

最佳答案

这里有一个可能的解决方案:

您需要为创建 NSPredicate 找到最小和最大纬度、经度值。

  1. 将度数转换为弧度

    -(float)deg2rad:(float)degrees{
    return degrees * M_PI / 180;
    }
  2. 找到最小和最大纬度、经度值//以公里(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 周围创建一个正方形,它可能对您有帮助。

关于ios - 我想根据距离和时间获取位置经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44063007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com