How to find the nearest 100-meter latitude and longitude depending on the current location in lens c, iphone

How to find the nearest 100-meter latitude and longitude depending on the current location, I created a database in SQLITE3, which consists of many latitudes and longitudes and corresponds to this location. Depending on the current location, I want to get the closest 100-meter latitude and longitude in lens C, I use 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; } 

how to get or how to calculate the surrounding 100 meters of latitude and longitude and display it.

Thanks for the help. I use 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; } 

and getting the following result 8505.369547 on return, I just got confused what the value that I really get.

+4
source share
2 answers

Perhaps the CoreLocation infrastructure can help you solve this problem? Your question seems to have made the following method:

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

You can check if the coordinate from your database falls into the area using the following method:

 - (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate 

I think this is not ideal if there are many places in your database. But perhaps you can filter out a set of locations for testing based on the minimum and maximum latitude / longitude values.


Edit: as requested, example (don't forget to import the CoreLocation environment):

 - (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: I understand that this is not the answer you asked, but it seems to me that this is what you need. As you can imagine, the nearest 100-meter latitude / longitude near any coordinate consists of an almost unlimited number of coordinates in a circular area. I assume that you want to find objects in your database that are near some coordinate, and the above code should help you with this. You can get a set of results from your database and check if they are near some coordinate.


Edit 3: The following code is most 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); 

Edit 4: regarding your question about a custom object to represent a table, use something like the following:

 @interface Location : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @end @implementation Location @synthesize name, coordinate; @end 
+3
source

Check out this link for some services related to geographic locations. can help you ..

http://www.geonames.org/export/ws-overview.html

0
source

Source: https://habr.com/ru/post/1393302/


All Articles