My question is: How can I get the locationManager coordinates to my viewDidLoad method to load my mapview with my previously checked position???
My locationManager lookin' like this (Strings for UIlabel):
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (wasFound) return;
wasFound = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
latitude.text = [NSString stringWithFormat: #"%f", loc.latitude];
longitude.text = [NSString stringWithFormat: #"%f", loc.longitude];
}
And my viewDidLoad like this:
CLLocationCoordinate2D theCoordinate1;
theCoordinate1.latitude = 37.786996;
theCoordinate1.longitude = -122.419281;
So I want to insert my real coordinates to the latitude and longitude places!
Thanks!
Make CLLocationCoordinate2D loc a property of whatever object your didUpdateToLocation: method is in. If it's the same object as the viewDidLoad code, then just use it directly. If it's in some other class, make a reference to that class available in your view controller and get the property that way.
Create a CLLocationCoordinate2D property and assign value to it.
Related
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:
I am trying to set a CLLocation property (named objectLocation) of a custom class, using the below code, called from my main ViewController. Unfortunately, I receive an error telling me on the "commented" line, that the "expression is not assignable." locationsArray is an array of objects of the custom class. I really need to set this value, so any help is appreciated!
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
for (int i = 0; i < [locationsArray count]; i++) {
if ([locationsArray[i] objectLocation] == NULL) {
[locationsArray[i] objectLocation] = [locations lastObject]; //retrieves most recent location data - THIS IS THE LINE THAT RECEIVES THE ERROR
//now set up a region and start monitoring data for that region
[locationsArray[i] region] = [[CLRegion alloc]
initCircularRegionWithCenter:
[[locationsArray[i] objectLocation] coordinate]
radius:2
identifier:[NSString stringWithFormat:#"%#", [locationsArray[i] objectLocationName]]];
}
}
}
If it is a property of CLLocation within your class and given that your locationsArray holds the object and your locations array does hold a CLLocation object, then please try the following:
((<YOUR_CLASS>*)[locationsArray[i]).objectLocation = [locations lastObject];
Hope this helps.
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;
}
This is my method
- (void)populateLocationsToSort {
//1. Get UserLocation based on mapview
self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
//Set self.annotationsToSort so any new values get written onto a clean array
self.myLocationsToSort = nil;
// Loop thru dictionary-->Create allocations --> But dont plot
for (Holiday * holidayObject in self.farSiman) {
// 3. Unload objects values into locals
NSString * latitude = holidayObject.latitude;
NSString * longitude = holidayObject.longitude;
NSString * storeDescription = holidayObject.name;
NSString * address = holidayObject.address;
// 4. Create MyLocation object based on locals gotten from Custom Object
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];
// 5. Calculate distance between locations & uL
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:self.userLocation];
annotation.distance = calculatedDistance/1000;
//Add annotation to local NSMArray
[self.myLocationsToSort addObject:annotation];
**NSLog(#"self.myLocationsToSort in someEarlyMethod is %#",self.myLocationsToSort);**
}
//2. Set appDelegate userLocation
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.userLocation = self.userLocation;
//3. Set appDelegate mylocations
myDelegate.annotationsToSort = self.myLocationsToSort;
}
In the bold line, self.myLocationsToSort is already null. I thought setting a value to nil was cleaning it out basically, ready to be re-used? I need to do so because this method is called once on launch and a second time after an NSNotification is received when data is gotten from the web. If I call this method again from the NSNotification selector, the new web data gets written on top of the old data and it spits out an inconsistent mess of values :)
Setting it to nil removes the reference to that object. If you are using ARC and it is the last strong reference to that object, the system autoreleases the object and frees its memory. In either case, it does not "clean it out and be ready for re-use", you need to re-allocate and initialize your object. If you would rather just remove all of the objects, and assuming myLocationsToSort is an NSMutableArray you can just call
[self.myLocationsToSort removeAllObjects];
Otherwise you need to do
self.myLocationsToSort = nil;
self.myLocationsToSort = [[NSMutableArray alloc] init];
In my app I fetch a coredatabase and put results into an array called self.stores. I convert those store locations to MyLocation objects which have a distance property. I plot the MyLocation objects like so:
- (void)plotStorePositions:(NSString *)responseString {
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
//CYCLE THRU STORE OBJECTS
for (Store * storeObject in self.stores) {
NSString * latitude = storeObject.latitude;
NSString * longitude = storeObject.longitude;
NSString * storeDescription = storeObject.name;
NSString * address = storeObject.address;
//CREATE MYLOCATION OBJECTS
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation *annotation = [[MyLocation alloc] initWithName:storeDescription address:address coordinate:coordinate distance:0];
//CREATE A PIN FOR EACH MYLOCATION ANNOTATION
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
//SET USERLOCATION (Must move this code out)
self.userLocation = [[CLLocation alloc] initWithLatitude:self._mapView.userLocation.coordinate.latitude longitude:self._mapView.userLocation.coordinate.longitude];
//SET USERLOCATION TO APPDELEGATE (Must move this code out)
AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
myDelegate.userLocation = self.userLocation;
//CALCULATE DISTANCE AND SET ITS THAT MYLOCATION's DISTANCE PROPERTY
CLLocationDistance calculatedDistance = [pinLocation distanceFromLocation:userLocation];
annotation.distance = calculatedDistance/1000;
[_mapView addAnnotation:annotation];
}
}
This is in a mapview. I then have a tableview where I fetch the same coredatabase and get self.stores again. Then I cycle through that array of self.stores to create STORE storeObjects to put into my tableview cells. But I want to sort those cells. How do I get MyLocation objects which should be in memory? Do I need to pass them to the appDelegate or to the tableview controller?
As you are using Core Data, your best option is to use a NSFetchedResultsController to populate your table view.
All you need then is the managed object context, either from another controller or the app delegate.