如何根据物镜c,Iphone中的当前位置找到最近的100米纬度和经度 [英] How to find the nearest 100 meters latitude and longitude depending upon the current location in objective c, Iphone

查看:137
本文介绍了如何根据物镜c,Iphone中的当前位置找到最近的100米纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据当前位置找到最近的100米纬度和经度,我在SQLITE3中创建了一个数据库,其中包含一组纬度和经度以及相应的位置名称。根据当前位置,我想在目标C中获取最近的100米纬度和经度,
我使用以下代码来获取当前位置,

How to find the nearest 100 meters latitude and longitude depending upon the current location, i have created a database in SQLITE3 which consists of set of latitudes and longitudes and corresponding there location name. Depending upon the current location i want to fetch the nearest 100 meters latitude and longitude in objective C, I am using the following code to get the Current location,

- (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米纬度和经度显示它。

so how to fetch or how to calculate the surrounding 100 meters latitudes and longitudes and display it.

感谢您使用以下公式的帮助

Thanks for the help I am using the following formula

     -(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,我只是困惑什么是我实际上得到的价值。

and getting the following output 8505.369547 on return, i am just confused what is the value i am getting actually.

推荐答案

也许CoreLocation框架可以帮助你解决这个问题?似乎以下方法是考虑到您的问题:

Perhaps the CoreLocation framework can help you with this issue? It seems the following method is made with your question in mind:

- (id)initCircularRegionWithCenter:(CLLocationCoordinate2D)center radius:(CLLocationDistance)radius identifier:(NSString *)identifier

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

You could test if a coordinate from your database falls within a region by using the following method:

- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate

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

I guess the above ain't ideal when your database contains a lot of locations. But perhaps one can filter out a set of locations to test based on some minimum and maximum latitude / longitude values.

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

as requested, an example (don't forget to import the CoreLocation framework):

- (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米纬度/经度由圆形区域​​中几乎无限量的坐标组成。我的猜测是你想在数据库中找到一些坐标附近的对象,上面的代码可以帮助你实现这个目标。您可以从数据库中检索一组结果,并测试它们是否在某个坐标附近。


Edit 2: I realize it's not exactly the answer you requested, but it seems to me this is what you need. As you can imagine the nearest 100 meters latitude / longitude near any coordinate consists of an virtually unlimited amount of coordinates in a circular region. My guess is you want to find objects within your database that are nearby some coordinate and the above code should help you achieve this. You can retrieve a set of results from your database and test if they are nearby some coordinate.

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

Edit 3: The following code is likely exactly what you need:

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:关于您对表视图的自定义对象的问题,请使用以下内容:


Edit 4: with regards to your question about the custom object for your table view, use something like this:

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


@implementation Location
@synthesize name, coordinate;
@end

这篇关于如何根据物镜c,Iphone中的当前位置找到最近的100米纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆