World View not finding location


#1

Hello,Forum.
IVe having a few more errors with this location program.Namely,
[worldView setShowsUserLocation:YES];
doesnt show me that blue dot on the map that was talked about and of course if i dont hav a location the “zoom” code wont work either…
This is my whereami.m incase anyone wants to try it and see how it works for them.
im not sure if i should have removed the “hack” by now or is my location getting passed or im i gettin these issues cause im using the sim. or what.
an y help would be welcomed thanks


#import "WhereamiAppDelegate.h"

@implementation CLLocationManager (TemporaryHack)

- (void)hackLocationFix
{
    CLLocation *location = [[CLLocation alloc] initWithLatitude:42 longitude:-50];
    [[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];     
}

- (void)startUpdatingLocation
{
    [self performSelector:@selector(hackLocationFix) withObject:nil afterDelay:0.1];
}

@end


@implementation WhereamiAppDelegate


@synthesize window=_window;


- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Create location manager object
    locationManager = [[CLLocationManager alloc] init];
       
    [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];
    //[locationManager locationServicesEnabled];
    
    
    [worldView setShowsUserLocation:YES];
    
    // This line may say self.window, don't worry about that
    [[self window] makeKeyAndVisible];
    return YES;
}

//-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

-(void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)u
{
    CLLocationCoordinate2D loc=[u coordinate];
    MKCoordinateRegion region=MKCoordinateRegionMakeWithDistance(loc, 250,250);
    [worldView setRegion:region animated: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);
}
- (void)applicationWillResignActive:(UIApplication *)application
{

#2

kemnet, I looked through your code you posted and I could not find anything in particular. I am receiving the blue dot so I thought I would post mine. See below.

From WhereamiAppDelegate.m:

[code]#import “WhereamiAppDelegate.h”

@implementation CLLocationManager (TemporaryHack)

  • (void)hackLocationFix
    {
    CLLocation *location = [[CLLocation alloc] initWithLatitude:42 longitude:-50];
    [[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];
    }

  • (void)startUpdatingLocation
    {
    [self performSelector:@selector(hackLocationFix) withObject:nil afterDelay:0.1];
    }

@end

@implementation WhereamiAppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    // Create location manager object
    locationManager = [[CLLocationManager alloc] init];
    [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];

    [locationManager startUpdatingHeading];

    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)locationManager:(CLLocationManager *)manager
    didUpdateHeading:(CLHeading *)newHeading
    {
    NSLog(@"%@", newHeading);
    }

  • (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);
    }

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

  • (void)applicationWillResignActive:(UIApplication *)application[/code]

From WhereamiAppDelegate.h:

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UITextField *locationTitleField;
}

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

@end
[/code]

Hopefully this helps you out! I haven’t finished the chapter though I am working on the zoom portion right now. From the looks of your code we might be at the same spot. I’m interested in hearing if you ever found the issue.


#3

kemnet if you haven’t solved this or for others.

I had the same issue but found out I hadn’t followed the steps correctly, all code was correct but I noticed I had missed the Reference worldView -> MKMapView.
Make sure your references is exactly as in Figure 5.4 in your XIB file.


#4

Thank you. I too had missed the instructions to make those connections.


#5

Where is Reference worldView -> MKMapView? I am stuck in the same spot. No blue dot, I rechecked figure 5.4 and I had missed the Return Key also, thanks for that. I don’t understand where the Reference worldVIew -> MKMapView is.

thanks for you help–your posts are most helpful


#6

Is it possible that you did not put a check in the showsUserLocation checkbox in the attributes inspector for your MapView instance? That would be the GUI way to turn that property on for the instance, without which it won’t show location, ever. Or you could set that instance variable programmatically, in your code. Cheers. Graham.


#7

Figured it out. It was something completely stupid. Just as far as the book goes, the book is completely correct in the code. It works just fine. The problem was, in the simulator, when the iphone pops up, in one of the menus there is a Location option. My “phone” didn’t have any location set to it so it wasn’t registering anything. Thanks all for your help!