【发布时间】:2011-12-01 23:03:39
【问题描述】:
情况如下,我在 MacOS 10.7 上运行 xCode 4.1,启动 iPhone 模拟器并使用 MapKit 试图获取 REAL 用户的坐标...默认用户的位置在美国库比蒂诺 .. . 要获得这一点的坐标,我使用:
self.mapView.userLocation.location.coordinate
...但这让我到了地图的中心(在非洲)...
问题 #1: 为什么???为什么用户的 pin 设置为 Cupertino,但其坐标是 (lat:0, lon:0) 在非洲?
……然后……
我已将我的控制器设置为 CCLocationManagerDelegate 和 MKMapViewdelegate,并将控制器初始化为 CCLocationManagerDelegate 事件的委托……但这些委托方法都没有被调用……
- (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
问题#2:为什么委托方法一次都没有触发?
我的控制器:
//@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]];
}
...
还有型号:
//@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;
}
谢谢,艺术
【问题讨论】:
-
如果
self.mapView.userLocation.location.coordinate给你 0,0 可能是因为 mapView 或 userLocation 或 location 是一个 nil 指针。