iphone - 如何根据 objective-c 中的当前位置找到最近的100米经纬度,Iphone

标签 iphone objective-c sqlite latitude-longitude

如何根据当前位置找到最近的 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,我只是很困惑我实际得到的值是多少。

最佳答案

也许 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:关于您关于 TableView 的自定义对象的问题,请使用如下内容:

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


@implementation Location
@synthesize name, coordinate;
@end

关于iphone - 如何根据 objective-c 中的当前位置找到最近的100米经纬度,Iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9029445/

相关文章:

iphone - 是否可以设置允许的最大 ios 版本

ios - 在标签栏中添加图像 (.gif)

iphone - iphone 无法解释的段错误

ios - 具有右图像和居中文本的 UIButton

SQLite for Android 自定义 TableView (SQL VIEW,不是 Android View )差异?

android - 在 Android 中管理 SQLite 连接

SQL 查询慢?应该吗?

iphone - iPad 自定义键盘 GUI

c# - 如何将事件推送到 iPhone 日历

iphone - 通用应用中的文档目录