I can initialize a CLLocation object by providing latitude and longitude like below:
CLLocation *location = [[CLLocation alloc] initWithLatitude:-43.242534 longitude:-54.93662];
How can I initialize a CLLocation object with not only latitude and longitude but also accuracy values?
As per apple docs you can use the following function:
double desired_horizontal_accuracy = 200.0 // in meters
double desired_vertical_accuracy = 200.0 // in meters
[[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(-43.242534,-54.93662)
altitude:-1
horizontalAccuracy:desired_horizontal_accuracy
verticalAccuracy:desired_vertical_accuracy
timestamp:[NSDate date]]
In the example, for the parameters altitude and timestamp, I put the same defaults as what the apple docs say are used on -initWithLatitude:longitude:
Related
I am working on a project in which I have to show the distance of multiple locations from one location. locations are based on latitude and longitude.
I am using the following code to get the distance between two locations is shows nearly same distance
CLLocation *locationA = [[CLLocation alloc] initWithLatitude:28.6379 longitude: 77.2432];CLLocation *locationB = [[CLLocation alloc] initWithLatitude:28.6562 longitude:77.2410];CLLocationDistance distance = [locationA distanceFromLocation:locationB];NSLog(#"Distance is %f",distance);float i = distance/1000;NSLog(#"distance between two places is %f KM", i);
but now i am struct to get the distance of multiple locations from my location: locationA.
for example I take NSArray for latitude and longitude as
NSArray * latitudeArray = [[NSArray alloc]initWithObjects:#"28.6129",#"28.6020",#"28.5244", nil];NSArray * longitudeArray = [[NSArray alloc]initWithObjects:#"77.2295",#"77.2478",#"77.1855", nil];
Please help me to resolve it..
Take locationA as one location..
Please help me to sort the Array by nearest Distance..
First of all, don't create two array for latitude and longitude, It should be one array of CLLocations.
NSMutableArray locationsArray = [[NSMutableArray alloc] init];
//This is just for example, You should add locations to this array according to format of data you have available.
[locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.6379 longitude:77.2432]];
[locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.6020 longitude:77.2478]];
[locationsArray addObject:[[CLLocation alloc] initWithLatitude:28.5244 longitude:77.1855]];
Now, Take some reference location,
CLLocation *yourLocationA ; //set whatever value you have..
You can sort array of location with following.
[locationsArray sortUsingComparator:^NSComparisonResult(CLLocation *obj1Location,CLLocation *obj2Location) {
CLLocationDistance obj1Distance = [obj1Location distanceFromLocation: yourLocationA];
CLLocationDistance obj2Distance = [obj2Location distanceFromLocation: yourLocationA];
return (obj1Distance > obj2Distance);
}];
I get user's current location with CLLocationCoordinate2D then would like to use it for Parse's nearGeoPoint.
I know I can just simply use geoPointForCurrentLocationInBackground but I'd rather retrieve with CLLocationCoordinate2D instead.
You can use the geoPointWithLatitude:longitude: initializer of the PFGeoPoint class. Give it the latitude and longitude values of your CLLocationCoordinate2D, and you'll have your PFGeoPoint.
e.g.
PFGeoPoint *geoPoint = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
I'm want to initialize a CLLocation object. To do this I'm using:
CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
My location need speed and timestamp, but speed and timestamp are read only parameters. How can I assign values to this parameters?
I have found the solution:
initWithCoordinate:altitude:horizontalAccuracy:verticalAccuracy:course:speed:timestamp:
More info: Apple doc for CLLocation.
I am creating an iOS application where I have some data structure containing multiple coordinates and I know the current user location. I want to fill the cells of the TableView with the distance from the user's current location to the locations in the TableView.
I was wondering when I should calculate the distance between the user's current location and all the locations in the TableView. Should I calculate all these distances when the application starts? What should I do when the user changes location? Set some sort of timer to check if the user's position changed and recalculate the distances every time the user's position changed?
I should also note, I am giving them the ability to sort this TableView by distance.
Thanks in advance.
You can user CLLocationManagerDelegate's locationManager:didUpdateLocations: Method.
This method is called when user's location will update so you can call your method to show data in tableview from user's current location.
below is some code for sorting your arryOfLocation.
for (int i = 0; i < arrySortByLocation.count; i++)
{
//Current Location Details
NSMutableDictionary *dict = [[arrySortByLocation objectAtIndex:i] mutableCopy];
CLLocationDegrees latitude = [dict[#"city_lat"] doubleValue];
CLLocationDegrees longitude = [dict[#"city_lng"] doubleValue];
CLLocation *location = [[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
CLLocationDistance distance = [appDele.userLocation distanceFromLocation:location];
dict[#"distance"] = [#(distance) stringValue];
//Storing as string since latitude and longitude is also string values
//Since its a dictionary storing as NSNumber is better
[arrySearchLocation setObject:dict atIndexedSubscript:i];
}
//sorting based on distance
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:#"distance.doubleValue" ascending:YES];
[arrySortByLocation sortUsingDescriptors:#[descriptor]];
}
Hope this will help you..
I have an array of CLLocation objects.
If I use
CLLocation *bestLocation = [locations valueForKeyPath:#"#min.horizontalAccuracy"];
I'm getting the lowest horizontalAccuracy value. But I want the OBJECT that contains the lowest value, so that I can later get it's proper coodinates. How can I do that?
I also tried with
CLLocation *bestLocation = [locations valueForKeyPath:#"#min.self.horizontalAccuracy"];
but got the same results.
Thanks
I don't think there is a simple Key-Value Coding method for that, but you can
loop over the array and keep track of the "best" element:
CLLocation *bestLocation = nil;
for (CLLocation *loc in locations) {
if (bestLocation == nil || loc.horizontalAccuracy < bestLocation.horizontalAccuracy)
bestLocation = loc;
}