如何根据目标c、Iphone中的当前位置找到最近的100米纬度和经度

2024-01-10

如何根据当前位置找到最近的 100 米纬度和经度,我在 SQLITE3 中创建了一个数据库,其中包含一组纬度和经度以及相应的位置名称。根据当前位置,我想获取目标 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

我想当您的数据库包含很多位置时,上述内容并不理想。但也许可以根据一些最小和最大纬度/经度值过滤出一组要测试的位置。


Edit:根据要求,一个示例(不要忘记导入核心位置框架):

- (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);        
        }
    }

}

Edit 2:我意识到这并不完全是您要求的答案,但在我看来这就是您所需要的。您可以想象,任何坐标附近最近的 100 米纬度/经度都由圆形区域​​中几乎无限数量的坐标组成。我的猜测是您想要在数据库中查找靠近某个坐标的对象,上面的代码应该可以帮助您实现这一目标。您可以从数据库中检索一组结果并测试它们是否位于某个坐标附近。


Edit 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);    

Edit 4:关于您关于表视图的自定义对象的问题,请使用如下内容:

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


@implementation Location
@synthesize name, coordinate;
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何根据目标c、Iphone中的当前位置找到最近的100米纬度和经度 的相关文章

  • XCode 无法将 iPhone 应用程序部署到 iPhone 3GS

    因此 我构建了我的 iPhone 应用程序 它在模拟器中运行良好 因此我想将其部署到我的 iPhone 上进行最后一轮测试 然后再使用我的分发配置文件进行公开 Beta 测试 我已经这样做过很多次了 从来没有出现过问题 然而 自从上次测试运
  • 如何将 UILabel 的值绑定到实例变量?

    我是 mac objective c 的新手 我的问题是 我想知道是否可以将 UILabel 文本绑定到变量 而不必在值更改时手动设置文本 例如 在 Mac OS 上 当我打开新的 Finder 窗口并删除文件时 任务栏中的全局可用空间就会
  • 在 iOS 上将 NSString 转换为 NSDate 的正确方法?

    我一直在使用此方法将常规 NSString 对象转换为 NSDate 但尝试向 Apple 提交更新 但遭到拒绝 在 iOS 中还有什么其他方法可以做到这一点 NSString date str 2011 08 12T12 20 00Z N
  • 如何使用 Swift 使用 TouchID?

    Apple 为 iOS 8 的 TouchID 实现提供的文档采用 Objective C 语言 有 Swift 版本吗 Objective C IBAction touchIDAvailable UIButton touchIDAvail
  • AVAssetExportSession 为零 iPhone 7 - Plus 模拟器

    AVAssetExportSession在 iPhone 6 及以下版本上运行良好 但在 iPhone 7 iPhone 7 Plus 模拟器上运行不佳 Xcode 8 0 这段代码return nil在exportSession中 当在i
  • insertNewObjectForEntityForName:

    我使用 Xcode xcdatamodel 文件编辑器设置了一个实体 我创建了一个名为 Person 的实体 添加了一些属性 然后生成了一个 m 文件来表示它 一切都很好 现在 当我去编写一行代码时 例如 Person person Per
  • 在 iPhone 中缝合图片

    我想并排缝合2张png 在Cocoa中 我会使用 NSImage initWithSize 然后只是drawInRect 但是 UIImage 没有 initWithSize 类 我现在该怎么做 Use UIGraphicsBeginIma
  • 如何阻止 UITableView moveRowAt IndexPath 在重新排序时留下空白行

    我遇到一个问题 在重新排序 UITableViewCells 时 tableView 不随单元格滚动 仅出现一个空白行 任何后续滚动都会出现数组越界错误 堆栈跟踪中没有我的任何代码 这是该问题的快速视频 http www screencas
  • UIImageJPEGRepresentation 在视网膜显示屏上提供 2x 图像

    我有这段代码 它创建一个图像 然后向其添加一些效果并缩小其大小以使其largeThumbnail UIImage originalImage UIImage imageWithData self originalImage thumbnai
  • 在 UIScrollview 上显示缩略图的最佳方法是什么(从服务器下载)

    我想在 UIScrollview 如照片应用程序 上显示许多图像 作为缩略图 所有图像将从服务器下载 据我所知 有几种选择 1 通过创建 UIImageviews 然后将它们添加为主滚动视图上的子视图 2 通过子类化一个UIView类 然后
  • SQLite .NET 性能,如何加快速度?

    在我的系统上 约 86000 个 SQLite 插入需要长达 20 分钟 意味着每秒约 70 个插入 我要做数百万 我怎样才能加快速度 对每一行的 SQLiteConnection 对象调用 Open 和 Close 会降低性能吗 交易有帮
  • Mac OS X 上的 .dll 等效项 [重复]

    这个问题在这里已经有答案了 我来自一个Windows背景 我习惯于通过创建 dll 然后分发所有库和文档 因此 如果用户想要使用它 他会添加对库的引用并使用它 但是 在 Mac 中 我正在开发 SDK 并且我想要一种方法creating a
  • 从 robovm 项目创建 iOS 静态库(JNI 中的 BAD_ACCESS)

    我有大量的Java代码 只有计算函数 没有UI 我想在iOS中将其重用为静态库 我的方法是使用robovm http www robovm com并按照robovm论坛中两篇文章中描述的非官方方式创建静态库 1 基本方式 https gro
  • 在 iOS 应用程序中拨打电话

    我有一些代码尝试在应用程序中进行调用 但它似乎不起作用 UIApplication myApp UIApplication sharedApplication NSString theCall NSString stringWithForm
  • ABAddressBookCopyArrayOfAllPeople 中缺少联系人

    我试图从我的应用程序中的地址簿中查找电话号码 但很惊讶没有找到它 问题是 我已经在我的应用程序访问的控制台中打印了地址簿的所有号码 奇怪的是 有些联系人丢失了 我正在将输出与我的地址簿进行比较 虽然数量很少 但仍然如此 这就是我访问地址簿的
  • Sqlite数据库生命周期?关闭应用程序后它会被删除吗?

    我正在遵循一个简单的教程 该教程创建一个从 SQLiteOpenHelper 扩展的类 并创建一个包含一个表和 5 行的数据库 好的 但我需要更多地了解 android Sqlite 数据库 例如 如果应用程序关闭或手机关机会发生什么 数据
  • BigQuery 未显示链接的 Firebase Analytics 事件日志的任何数据集

    我将我的帐户链接到 Big Query 但 Firebase Analytics 事件不会自动加载到 BigQuery 中 显示 未找到数据集 警告 我的工作进度附在下面 请查收 I have getting firebase Analyt
  • UITabBarController 为 TabBar 的每个 ViewController 提供不同的 Storyboard 文件

    我的团队正在开发一个具有 UITabBarController 的应用程序 我们正在使用 Storyboard 来开发界面和流程 由于我们是一个团队 所以我们不能将所有流程放在一个故事板中 因为这会导致与 SVN 同步出现问题 所以 解决方
  • 从超立方体图像中获取文本的确切位置

    使用 tesseract 中的 GetHOCRText 0 方法 我能够检索 html 中的文本 并在 webview 中呈现 html 时 我能够获取文本 但图像中文本的位置与输出不同 任何想法都非常有帮助 tesseract gt Se
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两

随机推荐