3

This is my CoreLocationController.h objective c class

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

extern const CLLocationAccuracy kCLLocationAccuracyBest;
@protocol CoreLocationControllerDelegate 
@required
- (BOOL)startRegionMonitoring;
- (void)locationUpdate:(CLLocation *)location; // Our location updates are sent here
- (void)locationError:(NSError *)error; // Any errors are sent here
@end

@interface CoreLocationController : NSObject <CLLocationManagerDelegate, MKMapViewDelegate>      {
CLLocationManager *locMgr;
CLLocationCoordinate2D coordinate;
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
id delegate;
} 

@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, assign) id delegate;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end

and this is the CorelocationController.m

#import "CoreLocationController.h"


@implementation CoreLocationController
@synthesize locMgr, delegate, coordinate;

- (id)init {
    self = [super init];

    if(self != nil) {
        self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
        self.locMgr.delegate = self; // Set the delegate as self.

        [locMgr setDistanceFilter:kCLDistanceFilterNone];
        [locMgr setDesiredAccuracy:kCLLocationAccuracyBest];

        [worldView setShowsUserLocation:YES];
    }

    return self;
}

- (BOOL)startRegionMonitoring {

    if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled] )
        return NO;
    CLLocationCoordinate2D home;
    home.latitude = +51.49410630;
    home.longitude = -0.10251360;

    CLRegion * region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:10.0 identifier:@"home"];
    if (locMgr == nil)
        locMgr = ([[CLLocationManager alloc] init]);
    [locMgr startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];

    [region release];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    NSLog(@"Enteed location");
//  [self leavingHomeNotify];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location entered" message:@"Region entered" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL];
    [alert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {  // Check if the class assigning itself as the delegate conforms to our protocol.  If not, the message will go nowhere.  Not good.
        [self.delegate locationUpdate:newLocation];

    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {     // Check if the class assigning itself as the delegate conforms to our protocol.  If not, the message will go nowhere.  Not good.
        [self.delegate locationError:error];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location failed" message:@"Location failed" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL];
    [alert show];
}
     - (void)dealloc {
    [self.locMgr release];
    [super dealloc];
}


@end

This is the CoreLocationDemoViewer.h

#import <UIKit/UIKit.h>
#import "CoreLocationController.h"
#import <MapKit/MapKit.h>

@interface CoreLocationDemoViewController : UIViewController <CoreLocationControllerDelegate, MKMapViewDelegate> {
    CoreLocationController *CLController;
    IBOutlet UILabel *locLabel;
    MKMapView *worldView;

}

@property (nonatomic, retain) CoreLocationController *CLController;
@property (nonatomic, retain) IBOutlet UILabel *locLabel;
@property (nonatomic, retain) MKMapView *worldView;

@end

This is the CoreLocationDemoViewer.m

#import "CoreLocationDemoViewController.h"


@implementation CoreLocationDemoViewController
@synthesize CLController;
@synthesize locLabel;
@synthesize worldView;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.worldView.mapType = MKMapTypeStandard;   // also MKMapTypeSatellite or MKMapTypeHybrid
    CLController = [[CoreLocationController alloc] init];
    CLController.delegate = self;
    [CLController.locMgr startUpdatingLocation];


    worldView.zoomEnabled = YES;
    worldView.scrollEnabled = YES;


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



- (void)locationUpdate:(CLLocation *)location {
    locLabel.text = [location description];
//  [mapView setCenterCoordinate:location.coordinate];
//  [mapView setShowsUserLocation:YES];

    CLLocationCoordinate2D coord = [location coordinate];

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

    // MKMapView retains its annotations, we can release
//  [mp release];

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

//   [locationManager stopUpdatingLocation];    
}

- (void)locationError:(NSError *)error {
    locLabel.text = [error description];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/



- (void)viewDidUnload {
    [super viewDidUnload];
     self.worldView = nil;

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [CLController release];
    [worldView release];
    [super dealloc];
}

@end

Ok so this is what i want. Until now it shows a map with the user location and it finds the exact location which is shown on a label.. correct if i am doing anything wrong..

What i want to do is trigger a video when a user reaches a certain location...

IAm approaching right?

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
hara
  • 27
  • 3
  • It looks as if you have everything set up right for the location pieces. Are you having issues with your code up to this point or just looking for validation of what you've done so far? – Bill Burgess Aug 09 '11 at 13:15

3 Answers3

1

I think if you are just having trouble getting the mapView to zoom correctly, use the MKCoordinate stuff from this post, How do I zoom an MKMapView.

I am working on a mapview as well that zooms to a decent level for viewing, and I am setting the lattitudeDelta and longitudeDelta for the map. Works good for me.

Community
  • 1
  • 1
Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
1

Perhaps something is wrong with setting the delegate? I usually specify the delegate in the .h file like this:

id <CoreLocationControllerDelegate> delegate;

and

@property (nonatomic, assign) id <CoreLocationControllerDelegate> delegate;

EDIT

Also, did you check that -mapView:didUpdateUserLocation: is being called?

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • i have just insert an NSLog to see if mapview is called and does not seem to appear.. what do i do??? NSLog(@"mapview is called"); – hara Aug 09 '11 at 17:27
0

I don't see you assigning the delegate for worldView (but you do for your other stuff). Try adding worldView.delegate = self; in your viewDidLoad.

Dylan Reich
  • 1,400
  • 2
  • 11
  • 14
  • I did it but no difference do you think that mapview is not called?? what can i do for that? thank you so muchhhhhhh i quite a newbee – hara Aug 09 '11 at 17:30
  • Try adding a breakpoint to where you think nothing is being called. If the breakpoint gets hit, then it's being called. If not, the method is not getting called. Just click on the line to the left of the editor to add a breakpoint (http://useyourloaf.com/blog/2011/2/21/xcode-breakpoint-actions.html). – Dylan Reich Aug 09 '11 at 20:06