I essentially started off with this CoreLocation iOS tutorial to get my head around how to implement CoreLocation into my app. (http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Location_Application)
But what I have encountered when trying to incorporate this tutorial into my app is that it now just keeps on looping over and over, which is just baffling me. Can anyone please help?
GPSViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface GPSViewController : UIViewController
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLLocation *startLocation;
#end
GPSViewController.m
#import "GPSViewController.h"
#import "DataClass.h"
#interface GPSViewController ()
#end
#implementation GPSViewController
#synthesize locationManager, startLocation;
DataClass *obj;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//initialization og global varable.
DataClass *obj=[DataClass getInstance];
//GPS Initialise
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
startLocation = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.latitude];
//latitude.text = currentLatitude;
obj.Latatude = currentLatitude;
NSString *currentLongitude = [[NSString alloc]
initWithFormat:#"%g",
newLocation.coordinate.longitude];
//longitude.text = currentLongitude;
obj.Longitude = currentLongitude;
NSLog(#"latitude %+.6f, longitude %+.6f\n",
newLocation.coordinate.latitude,
newLocation.coordinate.longitude);
if(obj.Latatude != NULL && obj.Longitude != NULL){
[self performSegueWithIdentifier:#"GPSSuccess" sender:self];
}
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.startLocation = nil;
self.locationManager = nil;
}
#end
You should call [locationManager stopUpdatingLocation]; in order to stop it from getting the user's location over and over.
Related
I try to update device's location course real time.
I am doing some experiment, and i make the VC as CLLocationManager's delegate. But when i run the app, the course information is not updating at all. i did set up a breakpoint in the delegate setting line, but the location manager is nil from the debugging area.
What is the problem?
#import "CourseInfomationViewController.h"
#interface CourseInfomationViewController ()
- (IBAction)startMoveLocation:(id)sender;
#end
#implementation CourseInfomationViewController
{
CLLocationManager *_locationManager;
CLLocation *_deviceLocation;
double _deviceDirection;
double _altitude;
double _speed;
}
-(id)init {
if (self = [super init]) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
//
_deviceLocation = [[CLLocation alloc] init];
}
return self;
}
- (IBAction)startMoveLocation:(id)sender {
[_locationManager startUpdatingLocation];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - location Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
_deviceLocation = [locations lastObject];
_altitude = _deviceLocation.altitude;
_deviceDirection = _deviceLocation.course;
_speed = _deviceLocation.speed;
NSLog(#"altitude is %f", _altitude);
NSLog(#"altitude is %f", _deviceDirection);
NSLog(#"altitude is %f", _speed);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"%#", error);
}
Update: Found this Then I am able from an Android device to spot the iOS beacon, but no App on the app store(searched for locate beacon) can find the beacon. Is iOS 9 having issues?
I am trying to use one device for advertising and one for monitoring.
I was expecting "didEnterRegion" to be called, but nothing - do you know why?
Project_1: Code for the advertising:
ViewController.h fil
#import <UIKit/UIKit.h>
#import CoreBluetooth;
#interface ViewController : UIViewController <CBPeripheralManagerDelegate>
#property (strong, nonatomic) CBPeripheralManager *peripheralManager;
#end
ViewController.m file
//
// ViewController.m
// MyBeacon
//
//
// 17BEFCE4-43B9-4921-BDAA-8361A24727A9
#import "ViewController.h"
// #import <CoreBluetooth/CoreBluetooth.h>
#import CoreLocation;
#import CoreBluetooth;
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUUID *proximityUUID = [[NSUUID alloc]
initWithUUIDString:#"39ED98FF-2900-441A-802F-9C398FC199D2"];
// Create the beacon region.
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
initWithProximityUUID:proximityUUID
identifier:#"com.mycompany.myregion"];
// Create a dictionary of advertisement data.
NSDictionary *beaconPeripheralData =
[beaconRegion peripheralDataWithMeasuredPower:nil];
// Create the peripheral manager.
CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
self.peripheralManager = peripheralManager;
// Start advertising your beacon's data.
[self.peripheralManager startAdvertising:beaconPeripheralData];
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Project_2: Code for monitoring:
ViewController.h file
//
// ViewController.h
// UseMyBeacon
//
//
#import <UIKit/UIKit.h>
#import CoreLocation;
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLBeaconRegion *region;
#end
ViewController.m
//
// ViewController.m
// UseMyBeacon
//
//
#import "ViewController.h"
#import CoreLocation;
#interface ViewController ()
#end
#implementation ViewController
- (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;
NSUUID *proximityUUID = [[NSUUID alloc]initWithUUIDString:#"39ED98FF-2900-441A-802F-9C398FC199D2"];
CLBeaconRegion *thisRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:#"com.mycompany.myregion"];
self.region = thisRegion;
Boolean b = [CLLocationManager isMonitoringAvailableForClass:[thisRegion class]];
if (b) {
// Create the beacon region to be monitored.
// CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc]
// initWithProximityUUID:proximityUUID
// identifier:identifier];
// Register the beacon region with the location manager.
self.locationManager.delegate = self;
[self.locationManager startMonitoringForRegion:thisRegion];
}
}
- (void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{
}
- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[self.locationManager requestStateForRegion:self.region];
}
- (void)locationManager:(CLLocationManager *)manager
rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region
withError:(NSError *)error{
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
}
- (void) locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
switch (state) {
case CLRegionStateInside:
[self.locationManager startRangingBeaconsInRegion:self.region];
break;
case CLRegionStateOutside:
case CLRegionStateUnknown:
default:
// stop ranging beacons, etc
NSLog(#"Region unknown");
}
}
- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ([beacons count] > 0) {
// Handle your found beacons here
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am trying to make a simple app the will get the users longitude and latitude
I followed the tutorial here:
http://www.appcoda.com/how-to-get-current-location-iphone-user/
and came with this:
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
#property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
- (IBAction)getCurrentLocation:(id)sender;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[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) {
_longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
#end
My issue is that "Would like to use your current location" popup.
and none of the delegate methods are being hit, at all. I put a break point at the beginning of each delegate method and nothing. Please help.
Please ensure you have following keys in your info.plist:
NSLocationAlwaysUsageDescription with value I need Location
NSLocationWhenInUseUsageDescription with value I need Location
privacy - location usage description with value I need Location
Ensure the info.plist details as answered earlier and then add the below code during your initialization;
if (IS_OS_8_OR_LATER) {
[locationmanager requestWhenInUseAuthorization];
//or (as per your app requirement)
[locationmanager requestAlwaysAuthorization];
}
here is my code, i am trying to get my current longtitude and latitude,
so my location will be display on map view.
however, the location manager delegate never get called, so i always getting longitude = 0.0000 and latitude = 0.0000.....
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController <CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property(retain,nonatomic)CLLocationManager *locationManager;
#property(assign,nonatomic)float longitude;
#property(assign,nonatomic)float latitude;
#end
and this is the implementation file:
#import "MapViewController.h"
#define METERS_PER_MILE 1609.344
#interface MapViewController ()
#end
#implementation MapViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self getCurrentLocation];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = self.latitude;
zoomLocation.longitude = self.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
[self.mapView setRegion:viewRegion animated:YES];
NSLog(#"longitu
de %f", self.longitude);
NSLog(#"latitude %f", self.latitude);
}
-(void)getCurrentLocation
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[self setMapView:nil];
[super viewDidUnload];
}
-(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 didUpdateLocations:(NSArray *)locations
{
NSLog(#"didUpdateToLocation: %#", [locations lastObject]);
CLLocation *currentLocation = [locations lastObject];
if (currentLocation != nil) {
self.longitude = currentLocation.coordinate.longitude;
self.latitude = currentLocation.coordinate.latitude;
NSLog(#"cal longitude %f", self.longitude);
NSLog(#"cal latitude %f", self.latitude);
}
}
#end
can anyone give me some advise please. cheers
May be you are running the code on simulator and SDK is below 6.0. In this case you get only 0.0000 as latitude and longitude. And make sure that application is granted access to device location.
If you just want to get current location then do the following :
-(void)getCurrentLocation
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
CLLocation currentLocation = self.locationManager.location;
[self.locationManager stopUpdatingLocation];
}
BTW, never use self when initialize any object. Because doing this creates 2 object.
first object is created by right side of this line self.locationManager = [[CLLocationManager alloc] init];. Second object is created when setter method is called by left side of the same line, to assign an object to locationManager variable.
I'm trying to create a helper class to get the coordinates of the phone in any other class easily. I've followed a tutorial in which the UIViewController implemented the <CLLocationManagerDelegate> and it worked. I tried to do the same in a simple NSObject, but then my delegate was not called anymore.
This is the code I have :
PSCoordinates.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface PSCoordinates : NSObject <CLLocationManagerDelegate>
#property (nonatomic, retain) CLLocationManager* locationManager;
#end
PSCoordinates.m
#import "PSCoordinates.h"
#implementation PSCoordinates
- (id) init {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(#"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Géolocalisation : %#",[newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(#"Géolocalisation (erreur) : %#",[error description]);
}
#end
I'm calling it by calling
PSCoordinates * coordinates = [[PSCoordinates alloc] init];
when pressing a button. The init is working as I can see the NSLog PSCoordinates init.
I've found other topics of people having the same problem but none of the answer solved it.
Your help would be really appreciated.
Make "PSCoordinates * coordinates" as global in your class. It will work :)