MKMapView didUpdateUserLocation not being called - objective-c

I am following the big nerd ranch guide book and have modified my app delegate.h to look like this:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#interface WhereamiAppDelegate : NSObject <UIApplicationDelegate,CLLocationManagerDelegate, MKMapViewDelegate>
{
IBOutlet UITextField *locationTitleField;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet MKMapView *worldView;
CLLocationManager *locationManager;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
#end
The .m looks like this:
#import "WhereamiAppDelegate.h"
#implementation WhereamiAppDelegate
#synthesize window = _window;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize managedObjectModel = __managedObjectModel;
#synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[worldView setRegion:region animated:YES];
NSLog(#"didUpdateUserLocation is called");
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Create location manager object
locationManager = [[CLLocationManager alloc] init];
// There will be a warning from this line of code; ignore it for now
[locationManager setDelegate:self];
// We want all results from the location manager
[locationManager setDistanceFilter:kCLDistanceFilterNone];
// And we want it to be as accurate as possible
// regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
// Tell our manager to start looking for its location immediately
// [locationManager startUpdatingLocation];
[worldView setShowsUserLocation:YES];
// This line may say self.window, don't worry about that
[self.window makeKeyAndVisible];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"%#", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Could not find location: %#", error);
}
When I open the app it should ZOOM into my location. But it doesn't zoom, so i put an NSLog in the didUpdateUserLocation to see if it was called. But it was never printed, so it wasn't called. How do I fix this?

Same thing happened to me...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[worldView setDelegate:self];
...
}
Thks

Related

showsUserLocation won't work for some reason

For some reason my Map wont present the Users location when button "GetLocation" is pressed. I also made sure "GetLocation" is delegated to the button on the viewcontroller. I have compared my code with others in this forum and been stuck on this matter for quite a while..
//ViewController .h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKFoundation.h>
#import <MapKit/MKAnnotationView.h>
#import <CoreLocation/CoreLocation.h>
#interface testmap : UIViewController <MKAnnotation, MKMapViewDelegate, CLLocationManagerDelegate> {
IBOutlet MKMapView *mapView;
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, copy) NSString *name;
#property(nonatomic, retain) IBOutlet MKMapView *mapView;
#property (strong) CLLocationManager *locationManager;
- (id)initWithLocation:(CLLocationCoordinate2D)coord;
- (IBAction)GetLocation:(id)sender;
#end
//ViewController .m
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "testmap.h"
- (NSString *)deviceLocation {
return [NSString stringWithFormat:#"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}
- (IBAction)GetLocation:(id)sender; {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
if (IS_OS_8_OR_LATER) {
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
mapView.showsUserLocation = YES;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.mapView.showsUserLocation = YES;
self.mapView.showsPointsOfInterest = YES;
}
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

CLLocation doesn't work - unrecognized selector sent to instance

I have a tabbar project in xcode and on the first view i need to find my GPS location and I need to save longitude and latitude on two variables on the appdelegate. Here some code:
FirstViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
#property (nonatomic, retain) CLLocationManager *locationManager;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "CampeggiandoAppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize locationManager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:#"ic_home2"];
self.tabBarItem.title=#"Home";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.distanceFilter=500.0f;
self.locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CampeggiandoAppDelegate *appDelegate=(CampeggiandoAppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.latitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.latitude];
appDelegate.longitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.longitude];
}
When I run the compiler and the fitst view comes up the application broke and appears this exception:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[AppDelegate
setLatitudineDel:]: unrecognized selector sent to instance 0x84210b0'
Any help? Thanks.
Your class interface should look like this before setting variables the way you do:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
// some ivars
// ...
}
#property (nonatomic, strong) NSString *latitudineDel;
#property (nonatomic, strong) NSString *longitudineDel;
#end
What you have for this moment is this:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
NSString *latitudineDel;
NSString *longitudineDel
// ...
}
#end
So there are no setters for those instance variables, that's why the exception is thrown.
For more information about properties read this.

Insert View controller before SplitViewController

I want to insert a RootViewController before my SplitViewController.
My code in AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[_window addSubview:[_viewController view]];
[_window makeKeyAndVisible];
return YES;
}
Delegate call to my rootviewcontroller and in this i want to call to my splitviewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
SplitViewController *splitViewController = [SplitViewController initApp];
[[self navigationController] pushViewController:splitViewController animated:YES];
}
In my SplitViewController i have this method:
+ (YetSplitViewController*) initApp
{
YetSplitViewController * cont = [[YetSplitViewController alloc] initWithNibName:#"YetSplitViewController" bundle:nil];
if (NO)
{
cont.splitWidth = 15.0;
}
return [cont autorelease];
}
After that not found good.......
My first code call from delegate to my SplitViewController:
#interface TemplateAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) IBOutlet UIWindow *window;
#property (readonly, nonatomic) IBOutlet YetSplitViewController *splitViewController;
#property (readonly, nonatomic) IBOutlet MasterViewController *rootViewController;
#property (readonly, nonatomic) IBOutlet DetailViewController *detailViewController;
#end
#implementation TemplateAppDelegate
#synthesize window = _window;
#synthesize splitViewController = _splitViewController;
#synthesize rootViewController = _rootViewController;
#synthesize detailViewController = _detailViewController;
- (void)dealloc
{
[_window release];
[_splitViewController release];
[_rootViewController release];
[_detailViewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (NO)
{
_splitViewController.splitWidth = 15.0;
}
[_window addSubview:_splitViewController.view];
[_window makeKeyAndVisible];
return YES;
}
#end

Custom curent location Annotation pin not updating with core location

Trying to add a custom pin for current location, However the location does not update. Even after setting setShowsUserLocation = YES;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if ([[annotation title] isEqualToString:#"Current Location"]) {
self.image = [UIImage imageNamed:[NSString stringWithFormat:#"cursor_%i.png", [[Session instance].current_option cursorValue]+1]];
}
However, if I set to return nil; everything works fine, but I lose the custom image. I really want to get this to work. Any help would be greatly appreciated.
As you've seen the setShowsUserLocation flag only shows the current location using the default blue bubble.
What you need to do here listen for location updates from the phone and manually reposition your annotation yourself. You can do this by creating a CLLocationManager instance and remove and replace your annotation whenever the Location Manager notifies its delegate of an update:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// update annotation position here
}
To reposition the coordinates, I have a class, Placemark, that conforms to the protocol MKAnnotation:
//--- .h ---
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Placemark : NSObject <MKAnnotation> {
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, retain) NSString *strSubtitle;
#property (nonatomic, retain) NSString *strTitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
- (NSString *)subtitle;
- (NSString *)title;
#end
//--- .m ---
#implementation Placemark
#synthesize coordinate;
#synthesize strSubtitle;
#synthesize strTitle;
- (NSString *)subtitle{
return self.strSubtitle;
}
- (NSString *)title{
return self.strTitle;
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c {
self.coordinate = c;
[super init];
return self;
}
#end
Then in my mapview controller I place the annotation with:
- (void) setPlacemarkWithTitle:(NSString *) title andSubtitle:(NSString *) subtitle forLocation: (CLLocationCoordinate2D) location {
//remove pins already there...
NSArray *pins = [mapView annotations];
for (int i = 0; i<[pins count]; i++) {
[mapView removeAnnotation:[pins objectAtIndex:i]];
}
Placemark *placemark=[[Placemark alloc] initWithCoordinate:location];
placemark.strTitle = title;
placemark.strSubtitle = subtitle;
[mapView addAnnotation:placemark];
[self setSpan]; //a custom method that ensures the map is centered on the annotation
}

error: expected specifier-qualifier-list before 'GKPeerPickerController

I keep getting this message (in the title). Just take a quick look at my code if you want to see what I'm doing. I've just started implementing the Peer Picker, so I'm not completely done yet. I just need some advice/help in the first part. The error shows up in the .m file between the two #import statements, which means it has to be some wrong way that I've used the GKPeerPickerController in the header file.
Bluetooth_Ad_Hoc_NetworkAppDelegate.h
#import <UIKit/UIKit.h>
#class Bluetooth_Ad_Hoc_NetworkViewController;
#interface Bluetooth_Ad_Hoc_NetworkAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Bluetooth_Ad_Hoc_NetworkViewController *viewController;
GKPeerPickerController *picker;
GKSession *session;
IBOutlet UILabel *status;
NSData *data;
}
#property(nonatomic, retain)IBOutlet UILabel *status;
#property(nonatomic, retain)GKPeerPickerController *picker;
#property(nonatomic, retain)GKSession *session;
#property(nonatomic, retain)IBOutlet UIWindow *window;
#property(nonatomic, retain)IBOutlet Bluetooth_Ad_Hoc_NetworkViewController *viewController;
#end
Bluetooth_Ad_Hoc_NetworkAppDelegate.m
#import "Bluetooth_Ad_Hoc_NetworkAppDelegate.h"
#import "Bluetooth_Ad_Hoc_NetworkViewController.h"
#implementation Bluetooth_Ad_Hoc_NetworkAppDelegate
#synthesize status;
#synthesize picker;
#synthesize session;
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
// allocate and initialize data
data = [[NSData alloc] initWithBytes:&status length:sizeof(status)];
// Allocate and setup peer picker controller
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
}
- (void)dealloc {
[status release];
[viewController release];
[window release];
[super dealloc];
}
#end
Have you included this statement in the header file?
#import <GameKit/GameKit.h>
Also you need to include the GameKit framework.