I am using the code as I specified below to get the altitude value
-(void)awakeFromNib {
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
heightMesurement.text = [NSString stringWithFormat: #"%.2f m", newLocation.altitude];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
heightMesurement.text = #"0.00 m";
}
The didUpdateToLocation method is calling and I am getting the altitude value as null when I print heightMesurement.text.
Can anyone give me idea why this happening like this and how to rectify it.
Thanks to all,
Monish.
Related
Hi everyone in my app i'm finding the iBeacons here the didExitRegion Method i called when i exit from the Region but the didEnterRegion method is not called when i entering the region.i fixed the background Refresh,remote notifications,location request always in info.plist
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
[self initRegion];
if([locManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[locManager requestAlwaysAuthorization];
}
// clBeconRegion.notifyOnEntry=YES;
clBeconRegion.notifyEntryStateOnDisplay=YES;
}
-(void)initRegion
{
NSUUID *uuid=[[NSUUID alloc]initWithUUIDString:UUID];
clBeconRegion=[[CLBeaconRegion alloc]initWithProximityUUID:uuid identifier:#"BeaconExample"];
// clBeconRegion=[[CLBeaconRegion alloc]initWithProximityUUID:uuid major:8983 minor:738
// identifier:#"aB"];
// clBeconRegion=[[CLBeaconRegion alloc]initWithProximityUUID:uuid major:8983 minor:728
// identifier:#"bB"];
clBeconRegion.notifyOnEntry=YES;
// clBeconRegion.notifyOnExit=YES;
clBeconRegion.notifyEntryStateOnDisplay=YES;
[locManager startMonitoringForRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region{
NSLog(#"didStartMonitoringFor Region");
// [locManager startRangingBeaconsInRegion:clBeconRegion];
[locManager requestStateForRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
switch (state) {
case CLRegionStateInside:
[locManager startRangingBeaconsInRegion:clBeconRegion];
NSLog(#"Region Inside");
break;
case CLRegionStateOutside:
// [locManager stopRangingBeaconsInRegion:clBeconRegion];
NSLog(#"Region OutSide %ld",(long)state);
case CLRegionStateUnknown:
default:
// stop ranging beacons, etc
NSLog(#"Region unknown %ld",(long)state);
}
}
-(void)locationManage:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(#"Entered Region");
[locManager startRangingBeaconsInRegion:clBeconRegion];
CLBeaconRegion *reg=(CLBeaconRegion *)region;
[disObj login:[NSString stringWithFormat:#"%#", reg.major] :[NSString stringWithFormat:#"%#", reg.minor]];
NSString *enterRegion=[NSString stringWithFormat:#"You ENTERED a Region %#",reg.minor];
[self sendLocalNotificationWithMessage1:enterRegion];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
CLBeaconRegion *reg=(CLBeaconRegion *)region;
NSString *exit=[NSString stringWithFormat:#"You Exit a Region %#",reg.minor];
[self sendLocalNotificationWithMessage1:exit];
NSLog(#"Exit Region");
[locManager stopRangingBeaconsInRegion:clBeconRegion];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{}
Check didEnterRegion signature: you wrote locationManage not locationManager
When I configure the CLlocationManager with this code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
// Or [self.locationManager requestWhenInUseAuthorization];
} [self.locationManager startUpdatingLocation];
}
and the set the location delegate
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
self.textLongitude.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
self.textLatitude.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
DonĀ“t send the alert for authorized, but the Info.plist is complete
when go to settings and change in settings -> privacy -> localize and change the status for "always", and restart the app this status is clean.
The NSLocationAlwaysUsageDescription is misspelled in the plist.
I am trying to get the Location "Latitude & Longitude".
The steps which I did as follow:
In CurrentLocationViewController.m:
#implementation CurrentLocationViewController {
CLLocationManager *locationManager;
}
Then in CurrentLocationViewController.h
#import <CoreLocation/CoreLocation.h>
#interface CurrentLocationViewController : UIViewController <CLLocationManagerDelegate>
Back to In CurrentLocationViewController.m:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
locationManager = [[CLLocationManager alloc] init];
}
return self;
}
- (IBAction)getLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError %#", error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation !=nil) {
self.longitudeLabel.text = [NSString stringWithFormat:#"%0.8f", currentLocation.coordinate.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:#"%0.8f", currentLocation.coordinate.latitude];
}
}
I put Break point on each methods, and even though, when I run the application, i got message in the Console: BSXPCMessage received error for message: Connection interrupted.
Any idea?
I got the same error message, still can't fix 'BSXPCMessage' error, but about the location part, I guess you're running iOS 7 version codes on iOS 8?
If so, try this:
Add [locationManager requestAlwaysAuthorization] or [locationManager requestWhenInUseAuthorization] above [locationManager startUpdatingLocation]
open Info.plist, add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription into 'information property list' with a message
That's it, hope this can help you.
Getting the same error message when working with AVFoundation. Images and videos. It's not specific to what you do.
How can i make my custom circle overlay follow as a subview the current user location in real time? I have managed to display my circle overlay and get to the center of userlocation but when user moves (gps) the overlay stays still and does not followuser. How do i fix that? Thank you in advance..
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setRegion:MKCoordinateRegionMake(self.location.coordinate, MKCoordinateSpanMake(0.02, 0.02))];
[self configureOverlay];
[[self locationManager] startUpdatingLocation];
}
in configureOverlay:
CircleOverlay *overlay = [[CircleOverlay alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addOverlay:overlay];
GeoQueryAnnotation *annotation = [[GeoQueryAnnotation alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addAnnotation:annotation];
I also tried using:
CLLocation *location = _locationManager.location;
CLLocationCoordinate2D coordinate = [location coordinate];
and replacing coordinate with self.location.coordinate
any advise would be appreciated.Thank you
Tell your class you are going to implement CLLocationManagerDelegate
#interface YourClass () <CLLocationManagerDelegate>
Then add the following methods:
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//You can change the overlay coordinates here. Even with an animation if you want.
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ? #"Access Denied" : #"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error getting Location"message:errorType delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
The locationManager:didUpdateToLocation... method will be triggered each time the location manager detects a change in the user's position.
Cheers.
EDIT
I forgot about something very important. In your viewDidLoad method add your class as the delegate:
self.locationmanager.delegate = self;
Problem is location manager is not updating location.
The (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc functions gets called but it displays the new location as the same old location.
Following is my piece of code:
In my viewDidLoad method i am creating the object of CLLocationManager
-(void) viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// created a timer to call locationUpdate method
[NSTimer scheduledTimerWithTimeInterval:20 target: self selector: #selector(locationUpdate) userInfo: nil repeats: YES];
}
-(void)locationUpdate
{
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude);
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLoc.coordinate, 0.01, 0.02);
[self.mapView setRegion:region animated:YES];
[self.locationManager stopUpdatingLocation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
The value which i am getting in NSLog(#"in locationmanager did update %f",newLoc.coordinate.latitude) is always the same even-though i had moved for more than 2 kilometers starting from current location.
Please help me to how to get exact new location whenever there is a locationupdate. Thanks in advance.
You are stopping the location manager with
[self.locationManager stopUpdatingLocation];
in
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
. This delegate method gets called EVERY time the user moves and can give you old (cached) data at the beginning, so by stopping it right away when you get your first location fix you probably get a cached location each time. The fix is simple, just don't stop the location manager there but rather when your viewController disappears or at a similar useful place.