如何在Objective C的iPhone上根据当前位置找到最近的100米纬度和经度

4
如何根据当前位置找到最近的100米纬度和经度,我已经在SQLITE3中创建了一个数据库,其中包含一组纬度和经度以及相应的位置名称。根据当前位置,我想在objective C中获取最近的100米纬度和经度。
以下是我使用的代码获取当前位置:
- (void)viewDidLoad {
    [super viewDidLoad];

    databaseName = @"searchdata.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    databasePath = [documentDir stringByAppendingPathComponent:databaseName];

    [self checkAndCreateDatabase];
    [self readDataFromDatabase];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    int degrees = newLocation.coordinate.latitude;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
    NSLog(@" Current Latitude : %@",lat);
    //latLabel.text = lat;
    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
    NSLog(@" Current Longitude : %@",longt);
    //longLabel.text = longt;
}

如何获取或计算周围100米的纬度和经度,并显示它。

感谢您的帮助,我正在使用以下公式

     -(double)kilometresBetweenPlace1:(CLLocationCoordinate2D) place1 andPlace2:(CLLocationCoordinate2D) place2 {

    double dlon = convertToRadians([pLong intValue] - [longt intValue]);
    double dlat = convertToRadians([pLat intValue] - [lat intValue]);

    double a = ( pow(sin(dlat / 2), 2) + cos(convertToRadians([lat intValue]))) * cos(convertToRadians([pLat intValue])) * pow(sin(dlon / 2), 2);
    double angle = 2 * asin(sqrt(a));
      return angle * RADIO;
}

获取以下输出8505.369547,当返回时,我不确定实际得到的值是什么。

我不确定我理解你的问题。你所说的“最近100米纬度和经度”是什么意思?你是在寻找数据库中距离某一点100米以内的坐标吗?请具体说明。 - sosborn
@sosborn 嗯,我正在寻找数据库中距离在100米以内的点。 - shasha
@开发者 答案中的编辑代码是正在工作的代码,我只使用那份代码。 - shasha
但我只需要一个位置,而且我没有任何数据库。 - Youngwing
@shasha,你能分享一下你的代码版本吗?我遇到了同样的问题。 - sunny k
显示剩余4条评论
1个回答

3
也许 CoreLocation 框架可以帮助您解决这个问题?似乎以下方法是为了回答您的问题而创建的:
- (id)initCircularRegionWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius identifier:(NSString *)identifier

您可以使用以下方法测试数据库中的坐标是否位于某个区域内:

您可以使用以下方式测试数据库中的坐标是否在区域内:

- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate

如果你的数据库包含大量位置信息,那么上述方法可能并不理想。但是也许可以基于最小和最大纬度/经度值筛选一组位置进行测试。


编辑:根据要求,这里提供一个示例(不要忘记导入CoreLocation框架):

- (void)startTest
{

#define COORDS_COUNT 4
#define RADIUS       10000.0f

    CLLocationCoordinate2D coords[COORDS_COUNT] = {
        CLLocationCoordinate2DMake(52.000004, 4.100002),
        CLLocationCoordinate2DMake(53.000009, 4.000003),
        CLLocationCoordinate2DMake(52.000001, 4.500008),
        CLLocationCoordinate2DMake(52.000002, 3.900900),
    };

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(52.0f, 4.0f); // Amsterdam
    CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:center radius:RADIUS identifier:@"Amsterdam"];

    for (int i = 0; i < COORDS_COUNT; i++)
    {
        CLLocationCoordinate2D coord = coords[i];
        if ([region containsCoordinate:coord])
        {
            NSLog(@"location %f, %f is within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);
        }
        else 
        {
            NSLog(@"location %f, %f is not within %.0f meters of coord %f, %f", coord.latitude, coord.longitude, RADIUS, center.latitude, center.longitude);        
        }
    }

}

编辑2:我知道这不完全是你要求的答案,但我认为这正是你需要的。你可以想象,在任何坐标附近最接近100米的纬度/经度由一个圆形区域中的几乎无限数量的坐标组成。我的猜测是,您希望在数据库中找到附近某个坐标的对象,上面的代码应该可以帮助您实现此目标。您可以从数据库中检索一组结果,并测试它们是否靠近某个坐标。


编辑3:以下代码可能正是您所需:

CLLocation *center = [[CLLocation alloc] initWithLatitude:52.0f longitude:4.0f]; // Amsterdam
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:52.000004f longitude:4.100002f];

CLLocationDistance distance = [center distanceFromLocation:location1];
NSLog(@"%f", distance);    
编辑4: 关于你对自定义对象的问题,可以像这样使用:

@interface Location : NSObject 
@property (nonatomic, strong) NSString               *name;
@property (nonatomic, assign) CLLocationCoordinate2D  coordinate;
@end


@implementation Location
@synthesize name, coordinate;
@end

你能帮我提供更多细节吗?比如说,如何实现你所说的方法。我有些困惑,是否还有其他方法或函数我需要添加,我应该将MapKit添加到项目中以实现你所说的方法吗?是否有任何示例或参考资料,以便我可以清楚地了解开发过程。这是我第一次开发基于位置的服务的应用程序。 - shasha
我正在使用您在Edit1中提供的代码来检查我的数据库中的纬度和经度是否在100米以内,在我的日志中,它正确地显示了哪些在100米以内,哪些不在100米以内,但是当我尝试在UITable视图上显示它时,它显示了所有的纬度和经度,但我只需要显示那些在100米以内的纬度和经度,如何实现这一点。 - shasha
将有效结果添加到数组中,并使用该数组作为表视图的数据源。 - Wolfgang Schreurs
我已经创建了数组并且数据已经存在其中,以下是我编写的代码:[resultArray addObject:coordString]; NSLog(@"data in resultArray %@",resultArray); 我得到的输出是"12.920684,77.560036",但我需要将相应的纬度和经度名称传递给表格,我该如何做。名称、纬度和经度值存储在我正在使用的数据库中。 - shasha
创建一个自定义对象,其中包含您需要的所有属性,并创建这些自定义对象的数组。 - Wolfgang Schreurs
我尝试创建自定义对象并传递,但没有得到预期的值。您能否请发送任何链接,以便我可以清楚地了解。 - shasha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接