Annotation problem


#1

My code for WhereAmI compiles and runs and properly displays the blue location marker. Once I enter text in the location title field, textFileShouldReturn fires ok, but the locationManager doesn’t seem to be picking up changes in location, so foundLocation is never called. The text field disappears and the activityIndicator keep spinning. I checked and rechecked the code and connections; all appears fine. Anyone see similar behavior? I am running Xcode 4.2.1 and iPhone 5.0 Simulator.


#2

It worked fine for me.

What’s happening inside:
(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation

Post your code so we can have a look.

mb


#3

Here is my code and xib connection summary. Thanks in advance for taking a look.

//======================
// WhereamiAppDelegate.h
//======================

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface WhereamiAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

  • (void)findLocation;
  • (void)foundLocation:(CLLocation *)loc;

@end

//=======================
// WhereamiAppDelegate.m
//=======================

#import “WhereamiAppDelegate.h”
#import “MapPoint.h”

@implementation WhereamiAppDelegate
@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [worldView setShowsUserLocation:YES];

    [[self window] makeKeyAndVisible];
    return YES;
    }

  • (void)mapView:(MKMapView *)wv didUpdateUserLocation:(MKUserLocation *)u
    {
    CLLocationCoordinate2D loc =[u coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
    }

[color=#FF0000]// NOT GETTING TRIGGERED IN DEBUG[/color]

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@", newLocation);
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    if (t < -180) {
    return;
    }

    [self foundLocation:newLocation];
    }

  • (void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location %@”, error);
    }

  • (BOOL)textFieldShouldReturn:(UITextField *)tf
    {
    [self findLocation];
    [tf resignFirstResponder];
    return YES;
    }

  • (void)findLocation
    {
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
    [locationManager startUpdatingLocation];
    }

  • (void)foundLocation:(CLLocation *)loc
    {
    CLLocationCoordinate2D coord = [loc coordinate];

    // Create an instance of MapPoint with the current data
    MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord
    title:[locationTitleField text]];

    //Add it to the map
    [worldView addAnnotation:mp];

    // MKView retains it annotations, we can release
    [mp release];

    // Zoom the region to this location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
    }

  • (void)applicationWillResignActive:(UIApplication *)application{}

  • (void)applicationDidEnterBackground:(UIApplication *)application{}

  • (void)applicationWillEnterForeground:(UIApplication *)application{}

  • (void)applicationDidBecomeActive:(UIApplication *)application{}

  • (void)applicationWillTerminate:(UIApplication *)application{}

@end

//============
/// MapPoint.h
//============
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint: NSObject
{
NSString *title;
CLLocationCoordinate2D coordinate;
}

// A new designated initializer for instances of MapPoint

  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

// A required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// A optional property from MKAnnotation
@property (nonatomic, copy) NSString *title;

@end

//===========
// MapPoint.m
//===========
#import “MapPoint.h”

@implementation MapPoint
@synthesize coordinate, title;

  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
    {
    self = [super init];
    if (self) {
    coordinate = c;
    [self setTitle:t];
    }
    return self;
    }

  • (void)dealloc
    {
    [title release];
    [super dealloc];
    }
    @end

MainWindow.xib -> Whereami App Delegate
Outlets
activityIndicator - Gray Activity Indicator
locationTitleField - Text Field
worldView - Map View
Referencing Outlets
delegate - Map View, Text Field


#4

One thing I notice off the bat is your property declaration for UIWindow *window is different than what I/the book has. You have:

@property (nonatomic, retain) IBOutlet UIWindow *window;

I have:
@property (strong, nonatomic) IBOutlet UIWindow *window;

mb

[quote=“cramos”]Here is my code and xib connection summary. Thanks in advance for taking a look.

//======================
// WhereamiAppDelegate.h
//======================

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface WhereamiAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

  • (void)findLocation;
  • (void)foundLocation:(CLLocation *)loc;

@end

//=======================
// WhereamiAppDelegate.m
//=======================

#import “WhereamiAppDelegate.h”
#import “MapPoint.h”

@implementation WhereamiAppDelegate
@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [worldView setShowsUserLocation:YES];

    [[self window] makeKeyAndVisible];
    return YES;
    }

  • (void)mapView:(MKMapView *)wv didUpdateUserLocation:(MKUserLocation *)u
    {
    CLLocationCoordinate2D loc =[u coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView setRegion:region animated:YES];
    }

[color=#FF0000]// NOT GETTING TRIGGERED IN DEBUG[/color]

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
    fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@", newLocation);
    NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

    if (t < -180) {
    return;
    }

    [self foundLocation:newLocation];
    }

  • (void)locationManager:(CLLocationManager *)manager
    didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location %@”, error);
    }

  • (BOOL)textFieldShouldReturn:(UITextField *)tf
    {
    [self findLocation];
    [tf resignFirstResponder];
    return YES;
    }

  • (void)findLocation
    {
    [activityIndicator startAnimating];
    [locationTitleField setHidden:YES];
    [locationManager startUpdatingLocation];
    }

  • (void)foundLocation:(CLLocation *)loc
    {
    CLLocationCoordinate2D coord = [loc coordinate];

    // Create an instance of MapPoint with the current data
    MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord
    title:[locationTitleField text]];

    //Add it to the map
    [worldView addAnnotation:mp];

    // MKView retains it annotations, we can release
    [mp release];

    // Zoom the region to this location
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
    }

  • (void)applicationWillResignActive:(UIApplication *)application{}

  • (void)applicationDidEnterBackground:(UIApplication *)application{}

  • (void)applicationWillEnterForeground:(UIApplication *)application{}

  • (void)applicationDidBecomeActive:(UIApplication *)application{}

  • (void)applicationWillTerminate:(UIApplication *)application{}

@end

//============
/// MapPoint.h
//============
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint: NSObject
{
NSString *title;
CLLocationCoordinate2D coordinate;
}

// A new designated initializer for instances of MapPoint

  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

// A required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// A optional property from MKAnnotation
@property (nonatomic, copy) NSString *title;

@end

//===========
// MapPoint.m
//===========
#import “MapPoint.h”

@implementation MapPoint
@synthesize coordinate, title;

  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
    {
    self = [super init];
    if (self) {
    coordinate = c;
    [self setTitle:t];
    }
    return self;
    }

  • (void)dealloc
    {
    [title release];
    [super dealloc];
    }
    @end

MainWindow.xib -> Whereami App Delegate
Outlets
activityIndicator - Gray Activity Indicator
locationTitleField - Text Field
worldView - Map View
Referencing Outlets
delegate - Map View, Text Field[/quote]


#5

Thanks for taking a look, mb.

Rewriting @property (nonatomic, retain) IBOutlet UIWindow *window;
as @property (strong, nonatomic) IBOutlet UIWindow *window;
made no change unfortunately.

Any ideas why the label and red marker are not getting created? I’m still perplexed.


#6

[quote=“cramos”]Thanks for taking a look, mb.

Rewriting @property (nonatomic, retain) IBOutlet UIWindow *window;
as @property (strong, nonatomic) IBOutlet UIWindow *window;
made no change unfortunately.

Any ideas why the label and red marker are not getting created? I’m still perplexed.[/quote]

You may want to check your code more closely. e.g. You have WhereAmIAppDelegate inheriting from NSObject instead of UIResponder.

mb


#7

First I thought I having a similar problem but it appears, that name tag field does only appear when taping on (inbetween there are two on my device) red needle(s).

I am running this under iOS 5 on 3GS, used templates from here and running Xcode 4.2.1.

Discovering the following warning on the line [worldview addAnnotation:mp];

.../Whereami/WhereamiAppDelegate.m: warning: Semantic Issue: Sending 'MapPoint *' to parameter of incompatible type 'id<MKAnnotation>'

[code]- (void)foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = [loc coordinate];
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:coord title:[locationTitleField text]];
[worldview addAnnotation:mp]; // <=
[mp release];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldview setRegion:region animated:YES];

[locationTitleField setText:@""];
[activityIndicator stopAnimating];
[locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];
}[/code]

any ideas ? I downloaded the complete samples and loaded it into Xcode. Told me to apply changes regarding LLVM… maybe again the differences to Xcode 4?


#8

Are you sure that MapPoint conforms to the MKAnnotation protocol?


#9

That was the problem. Thank you :slight_smile: