This tutorial on mobileorchard.com uses 2 classes (or 2 sets of .h and .m) to implement core location. Can I just use everything there in my existing class? How would I do that?
Can I use my existing app delegate as a location delegate as well?
Also, is the
- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // send loc updates to myself
}
return self;
}
method the same as the usual initWithNib?
I'm trying to quickly implement something based on location information. As much help describing the above linked tutorial would be helpful.
Thanks. No - really, Thank You.
Yes, sure. You can make your delegate a location manager delegate too. You'll just need to implement CLLocationManagerDelegate there with the method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
to get location updates.
Related
I need to fetch the user current location for the WatchKit App in watchOS 2. How do I do this?
The other answers are incorrect. You can request location directly on the watch for watch OS2. The available method is called requestLocation. It allows you to request a single location update.
E.g.
#import <CoreLocation/CoreLocation.h>
#interface className : WKInterfaceController<CLLocationManagerDelegate>
Then where you want to request location:
self.locationManager = [CLLocationManager new];
self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestLocation];
Then you will get a single callback in one of the following CLLocationManagerDelegate methods.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// Callback here with single location if location succeeds
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
// Error here if no location can be found
}
See the WWDC15 video "What's New in Core Location" - https://developer.apple.com/videos/wwdc/2015/?id=714
I am new to the world of iBeacons and Objective-C so please bear with me
I have a very simple app built now that updates a Label when the device approaches an iBeacon. What is the Objective-C code sending the device to an external URL?
For example, as you approach the iBeacon, the device is taken to a Youtube video with an explanation of what you ware seeing.
Any tips are greatly appreciated.
Here is code that will do what you want, but it will only work if the app is in the foreground:
CLLocationManager *_locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:[[NSUUID alloc] initWithUUIDString:#"2F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"] identifier: #"any-radius-networks-ibeacon-default-uuid"];
[_locationManager startMonitoringForRegion:region];
_locationManager.delegate = self;
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.radiusnetworks.com/radbeacon/"]];
}
You'd have to add extra logic to make it open special URLs for special iBeacons.
Here we have to update the current location by using the location manager,and its correctly updating on simulator but its not updating on device only 4 or 5 times only repeated.why the location is not updated frequently kindly hep me to solve this problem
Here i used code is showed below
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
Delegate Method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(#"newLocation: %#",newLocation);
}
This is the delegate method is calling four or five times.
If you are using location accuracy as 'kCLLocationAccuracyBestForNavigation', your device should remain plugged in.
Better you set it as 'kCLLocationAccuracyBest'. Also move to significant distance to hit the delegate. initially it will hit 4-5 times to locate you. Once your location is identified, it will call the delegate when there will be change in your location.
So, in delegate write something in file stored in documents directory in append mode. log the lat/long and Time details into it. Now move for some distance.
Join the mobile and get the file from documents directory. It must have logg the required details.
Here i solved this problem using this delegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations{
}
I have a getLocation method that initializes my CLLocationManager and calls startUpdatingLocation. After I get the user's location, I call stopUpdatingLocation.
Later, when the user presses refresh, I call a method reloadData which calls getLocation so I can get the user's latest location. However, this never calls the locationManager:didUpdateToLocation:fromLocation
method.. so I never get the user's latest location. What could be the issue?
-(void) getLocation {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void) locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (oldLocation!= nil) { // make sure to wait until second latlong value
[self setLatitude:newLocation.coordinate.latitude];
[self setLongitude: newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;
[self makeUseOfLocationWithLat:[self getLatitude] andLon:[self getLongitude]];
}
}
-(void) reloadData {
[self getLocation];
}
Is it really necessary to allocate a new CLLocationManager? Try just allocate it once (in your init for example) and just call startUpdatingLocation and stopUpdatingLocation on demand.
For me, this solution works great.
Do you move during testing this? Because I think the callback will only be triggered, when:
you call startUpdatingLocation
your location changes
the location manager gets better results (more detailed)
So I think for your use-case: as you don't move, the callback locationManager:didUpdateToLocation:fromLocation: will only be called once after you hit refresh and as there is no old location, you will not go into the if case.
Another thing: you should definitely have only one CLLocationManager in your class. I only use one per project/application (wrapped in a singleton). This is the preferred way to do! Also I think this will retain the oldLocation value so that your problem may be resolved by changing this.
Best,
Christian
you can try this ,this will help u to update for every time when open the page.Cause if u put startUpdatingLocation in viewdidload ,it will get load only for the first time
-(void)viewWillAppear:(BOOL)animated
{
[locationManager startUpdatingLocation];
}
I'm trying to get heading info from CLLocationManager but it's not getting called. I did everything as the documentation says, but something is very wrong.
I'm using locationManager delegating into my app delegate.
Here is the method to retrieve heading messages:
- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSLog(#"%#", newHeading);
}
Here is the part from main()
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager startUpdatingHeading];
But nothing happens! With debugging, NSLog is never getting called.
When I do same with [locationManager startUpdatingLocation] everything works fine, shows the location info, using another method (very same looking but using
- (void) locationManager:(CLLocationManager *) manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
but I need heading info here.
Found the deal.
Code is perfectly fine, the issue is in Iphone simulator.
Somehow it is providing location info just fine, but heading info is not provided thus the message is never sent.
Talk about weird things, apple.