CLLLocationManager inside NSObject - ios

I am trying to create a NSObject that will get the location and the address, I will also be running some other code. I want to just throw it into my different apps. I am having issues with it updating the location. I have the protocol and delegate working, but I will post all my code to try to make it clear.
In my getLocation.h
#protocol getLocationDelegate
- (void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude;
#end
#interface getLocation : NSObject <CLLocationManagerDelegate> {
CGFloat latitude;
CGFloat longitude;
NSString *address;
}
#property (nonatomic, strong) id<getLocationDelegate> delegate;
#property (nonatomic, retain) CLLocationManager *locationManager;
- (void)getLocationInfo;
In my getLocation.m
- (id)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
- (void)getLocationInfo {
[self.locationManager startUpdatingLocation];
// This is being called but not starting the locationManager
}
After I get the address and the location I call the protocol like this, in my getLocation.h
[self.delegate getLocation:address getLongitude:longitude getLatitude:latitude];
In my .m file that I want to get the location this is the code
getLocation *location = [[getLocation alloc] init];
[location setDelegate:self];
[location getLocationInfo];
Then I have my method when the delegate gets called
-(void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude {
// Do code after location and address has been returned
}
Now my protocol and delegate are working what my problem is I can't get the locationManager to start updating. Not sure whats going on.

I figured it out, ARC was releasing getLocation *location = [[getLocation alloc] init]; before CoreLocation was starting. So I just needed to declare it in my .h and have it be strong.

Related

IOS CLLocation manager returning zero speed

Hi I want to calculate speed within my camera view in XCODE8. The code does not generate error but my speed is returning zero. So I searched the link below :Self Location Manager Delegate error in Xcode 6. My code has two interface files, one for CvVideoCamera and one for ClLocationManager. I never had two interface files in my project, so I am not sure I am doing everything correctly. My code is below, Can you please tell me if something is wrong in my code architecture that causing me to get zero speed.
viewcontroller.h
#interface ViewController : UIViewController<CvVideoCameraDelegate>
#property (nonatomic, strong) CvVideoCamera* videoCamera;
…
#end
#interface LocationController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager*locationManager;
#end
viewcontroller.m
#import "ViewController.h"
int speed;
#interface LocationController() {
}
#end
#implementation LocationController
#synthesize locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = (id)self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = locations.lastObject;
loc = locations.lastObject;
speed = loc.speed * 2.236;
}
#end
#interface ViewController (){
….
}
#end
#implementation ViewController
#synthesize imageView;
#synthesize videoCamera
- (void)viewDidLoad
{
[super viewDidLoad];
self.videoCamera.delegate = self;
(void)processImage:(cv::Mat&)image {
NSLog(#"speed: %.d”, speed)}

MKMapView mathod 'didUpdateUserLocation' not calling

I have this code in my file,
#synthesize myLocation;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager = [[CLLocationManager alloc] init];
myLocation.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
[myLocation setShowsUserLocation: YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.myLocation setRegion:[self.myLocation regionThatFits:region] animated:YES];
}
But, method didUpdateUserLocation not calling.
I already added required frameworks in app.
I added everything in info.plist file. But still it is not calling.
This is my .h file,
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface MapView : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *myLocation;
#property(nonatomic, retain) CLLocationManager *locationManager;
#end
I just solved the problem,
locationManager = [[CLLocationManager alloc] init];
myLocation.delegate = self;
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
[myLocation setShowsUserLocation: YES];
Use setShowUserLocation with value YES after startUpdatingLocation
You have not called the method to update the location yet so the delegate method is not getting called. You need to call method for startUpdatingLocation.
For Objective - C:
[locationManager startUpdatingLocation];
For Swift:
locationManager.startUpdatingLocation()
Brother I tried your code.The delegate method is not called.
Then I found out the solution you did not add below line at the end of the viewDidLoad method. Please add
[self.locationManager startUpdatingLocation];
After I added above line code in viewDidLoad method,the delegate method called successfully.

Where to store app location data?

I have an app that needs to retrieve the longitude/latitude coordinates of the device when it's opened, and then communicate with the server using these coordinates. My app is based on a UITabBarController so has different pages, some of which need to use this location data. I also need to implement pull to refresh on some of these pages.
What I need to know is: Where should I store the location coordinates and where should I implement the CLLocationManager? Should I have a location manager inside each class that needs location, or should it be in the app delegate?
Thanks
Just create a singleton location manager class (say, MyLocationManager). Your .h file can look like this:
#interface MyLocationManager : NSObject <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocation *currentLocation;
+ (id)sharedInstance;
- (void)updateCurrentLocation;
#end
Then in the .m file implement the methods:
#import "MyLocationManager.h"
#interface MyLocationManager ()
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
#implementation MyLocationManager
#pragma mark - Singleton methods
+ (id)sharedInstance {
static dispatch_once_t token;
static id shared = nil;
dispatch_once(&token, ^{
shared = [[super alloc] initUniqueInstance];
});
return shared;
}
- (id)initUniqueInstance {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyKilometer];
self.locationManager.delegate = self;
}
return self;
}
#pragma mark - Public methods
- (void)updateCurrentLocation {
[self.locationManager startUpdatingLocation];
}
#end
Implement the delegate methods and update the currentLocation property when you get the updated value. I'd also advice to check for geolocation permissions somewhere and react appropriately. And keep in mind that you should use notifications (NSNotificationCenter) to inform other objects about updates to singleton's properties, not delegation (generally, you shouldn't tie your singleton to any object). Good luck!

objective-c locationManager as delegate, be notified if location changed

I sourced my locationManager out in a own class and file
Now I want to be notified when the location is updated. So I tried to implement the delegation pattern. But for some reason it does not work. What I did:
In Location.h: specified class, protocol, ivar and property id, delegation method
In Location.m: added a call to the delegation method
In ViewController.h: added delegation protocol
In ViewController.m: implemented the delegation method
Build and run the code. But the delegation method in ViewController.m is not called :(
Any ideas what i missed?
Output
2013-07-04 11:55:30.429 Sandbox2[2001:c07] Inside Lokation::getLocation
2013-07-04 11:55:30.443 Sandbox2[2001:c07] Inside Lokation::locationManager
2013-07-04 11:55:30.449 Sandbox2[2001:c07] currentLat: 51.509980 currentLng -0.133700
// Missing NSLog Statement of the delegate method didFindLocation...
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
// added Lokation header
#import "Lokation.h"
// added LokationDelegate
#interface ViewController : UIViewController <CLLocationManagerDelegate, LokationDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) Lokation *lokation;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[self getLocation];
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
// added delegate function of Lokation
// Should be run after the locationmanager found a location
-(void)didFindLocation {
NSLog(#"I am found a lokation and I am now in the ViewController");
}
Lokation.h (20130704 updated to working code)
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
// Added class, protocol and method for the delegate
#class Lokation;
#protocol LokationDelegate <NSObject>
-(void)didFindLocation;
#end
#interface Lokation : NSObject <CLLocationManagerDelegate> {
CLLocationDegrees currentLat;
CLLocationDegrees currentLng;
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *currentLoc;
// added delegate property
#property (assign, nonatomic) id<LokationDelegate> delegate;
-(void)getLocation;
#end
Lokation.m (20130704 updated to working code)
#import "Lokation.h"
#implementation Lokation
#synthesize locationManager = _locationManager;
#synthesize currentLoc = _currentLoc;
-(void)getLocation {
NSLog(#"Inside Lokation::getLocation");
// active location determination
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// We don't want to be notified of small changes in location,
// preferring to use our last cached results, if any.
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Inside Lokation::locationManager");
if (!oldLocation ||
(oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {
currentLat = newLocation.coordinate.latitude;
currentLng = newLocation.coordinate.longitude;
} else { // oldLocation
currentLat = oldLocation.coordinate.latitude;
currentLng = oldLocation.coordinate.longitude;
}
self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];
NSLog(#"currentLat: %f currentLng %f", currentLat, currentLng);
// added call to the delegate function
[self.delegate performSelector:#selector(didFindLocation)];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(#"%#", error);
}
Hmm..i think you are doing right but if i do change like below:
remove __unsafe_unretained id<LokationDelegate> delegate; and replace #property (assign, nonatomic) id<LokationDelegate> delegate with #property (retain) id<LokationDelegate> delegate (and synthesize property),it works like charm for me

iOS Route-Me: Add User Location Marker

I have been trying to add the following things to a Route-Me app.
Add a marker at the starting location
Move the map to the user location
Move the marker to that new location
I am using the basic example from MapBox ios example, as my maps are from an offline mbtiles store.
This is my header file:
#import <UIKit/UIKit.h>
#import "RMMapView.h"
#import "RMMarker.h"
#import "RMMapViewDelegate.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#interface MBTiles_ExampleViewController : UIViewController
{
RMMapView *mapView;
}
#property (nonatomic, retain) IBOutlet RMMapView *mapView;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) CLLocation *currentLocation;
#property (nonatomic, retain) RMMarkerManager *markerManager;
#property (nonatomic, retain) RMMarker *locationMarker;
#end
And this is my implementation file:
#define kStartingLat 30.0f
#define kStartingLon -10.0f
#define kStartingZoom 1.5f
#import "MBTiles_ExampleViewController.h"
#import "RMMBTilesTileSource.h"
#import "RMMapContents.h"
#import "RMMarker.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#implementation MBTiles_ExampleViewController
#synthesize mapView;
#synthesize currentLocation;
#synthesize locationManager;
#synthesize markerManager;
#synthesize locationMarker;
(void)viewDidLoad
{
CLLocationCoordinate2D startingPoint;
startingPoint.latitude = kStartingLat;
startingPoint.longitude = kStartingLon;
NSURL *tilesURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"control-room-0.2.0" ofType:#"mbtiles"]];
RMMBTilesTileSource *source = [[[RMMBTilesTileSource alloc] initWithTileSetURL:tilesURL] autorelease];
[[[RMMapContents alloc] initWithView:self.mapView
tilesource:source
centerLatLon:startingPoint
zoomLevel:kStartingZoom
maxZoomLevel:[source maxZoom]
minZoomLevel:[source minZoom]
backgroundImage:nil] autorelease];
mapView.enableRotate = NO;
mapView.deceleration = NO;
mapView.backgroundColor = [UIColor blackColor];
mapView.contents.zoom = kStartingZoom;
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
UIImage *iconImage = [UIImage imageNamed:#"marker.png"];
locationMarker = [[RMMarker alloc] initWithUIImage: iconImage];
[markerManager addMarker: locationMarker AtLatLong: startingPoint];
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[mapView moveToLatLong:newLocation.coordinate];
RMLatLong newCoords = {newLocation.coordinate.latitude, newLocation.coordinate.longitude};
if (nil != markerManager)
[markerManager moveMarker:locationMarker AtLatLon: newCoords];
}
(void)dealloc
{
[mapView release];
[super dealloc];
}
#end
The marker.png has been added to my resources folder.
So my questions
Why is my starting marker not showing?
I am using xcode on SnowLeopard, so can the simulator actually find my location? As the map does not move.
Any help would be great as I have tried so many code snippets and tutorials but none have ended up working.
Regarding your second question, from the apple docs:
In Xcode 4.0 and 4.1, you could simulate only the current location in your application. As of Xcode 4.2, you can simulate locations other than your current location in iOS applications that use Core Location. To set a location, choose Edit Scheme from the scheme selector in the toolbar, select the Run action, and click the Options tab. You can then choose a location from the Location menu
http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html
I was able to get it working after cleaning marker after move.
marker = [[RMMarker alloc] initWithUIImage:[UIImage imageNamed:#"marker.png"] anchorPoint:CGPointMake(0.5f, 1.f)];
[mapView.contents.markerManager addMarker:marker AtLatLong:locPoland];
[mapView.contents.markerManager moveMarker:marker AtLatLon:locWawa];
[mapView.markerManager moveMarker:marker AtLatLon: locUser];
[mapView moveToLatLong:locUser];
marker = nil;

Resources