the situation is following, i am running xCode 4.1 on MacOS 10.7, starting iPhone simulator and using MapKit am trying to get REAL user’s coordinates … default user’s location is in Cupertino, USA … to get cordinates of this point i use :
self.mapView.userLocation.location.coordinate
… but this gets me to the center of the map (in Africa) …
Question #1 : Why??? Why user’s pin is set to Cupertino but its coordinates are (lat:0, lon:0) in Africa?
… then …
I have setup my controller as CCLocationManagerDelegate and MKMapViewdelegate and initialized controller as delegate for CCLocationManagerDelegate’s events … but neither of these delegate methods was called …
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
Question #2 : Why delegate methods are not triggered even once?
My controller :
//@interface MapController : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate>
//@property (nonatomic, retain) MapModel *mapModel;
//@property (nonatomic, retain) IBOutlet MKMapView *mapView;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{ ... }
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{ ... }
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{ ... }
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapModel = [[[MapModel alloc] init:self] autorelease];
[self.mapView setShowsUserLocation:YES];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
[self.mapView setMapType:MKMapTypeStandard];
CLLocationCoordinate2D x =
self.mapView.userLocation.location.coordinate; // (0,0) why???
//[self.mapView setRegion:xx animated:YES];
//[self.mapView regionThatFits:self.mapModel.region];
[[self.mapView userLocation] setCoordinate:[x coordinate]];
}
...
And model :
//@property (nonatomic, copy) NSString *user;
//@property (nonatomic, copy) NSString *caption;
//@property (nonatomic, copy) NSString *description;
//@property (nonatomic, assign) MKCoordinateRegion region;
//@property (nonatomic, retain) CLLocationManager *manager;
- (id)init :(id)delegate
{
self.manager = [[[CLLocationManager alloc] init] autorelease];
self.manager.delegate = delegate;
self.manager.distanceFilter = kCLDistanceFilterNone;
self.manager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.manager startUpdatingLocation];
MKCoordinateRegion region = {{0, 0}, {0.2, 0.2}};
self.region = region;
return self;
}
Thanks, Art
The default location in the simulator is Cupertino. In the latest Xcode (4.2, I believe), you can set the location of the simulator from a menu of prepopulated locations (to which you can add your own simply by including a GPX file in your project).
If you run on the device AND ENABLE LOCATION SERVICES FOR THE APP, you should get a valid location using your code.