【问题标题】:Get _correct_ coordinates of user MapKit on simulator在模拟器上获取用户 MapKit 的_正确_坐标
【发布时间】: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 指针。

标签: iphone ios xcode mapkit


【解决方案1】:

模拟器中的默认位置是库比蒂诺。在最新的 Xcode(我相信是 4.2)中,您可以从预先填充的位置菜单中设置模拟器的位置(您可以通过在项目中包含 GPX 文件来添加自己的位置)。

如果您在设备上运行并为该应用启用位置服务,您应该使用您的代码获取有效位置。

【讨论】:

  • 好的,我知道 Cupertino 是预设位置,但为什么蓝色用户的标记在 Cupertino 而 mapView.userLocation 在非洲?为什么它们不同?
  • @Art 检查 mapView.userLocation 是否为nil
  • 我没有答案。您是否在设备上或 Xcode 4.2 中尝试过(将位置设置为非 Cupertino 的位置)?
  • @XcodeDev 所说的。我认为模拟器在 Xcode 4.2 之前没有“启用”定位服务,因此你得到一个 nil 位置(即使它在 CUP 中显示位置)。
  • 它不为空...首先调用 userLocation 如下 - 纬度:2.9960885786808235e-282,经度:1.6178140217591351e-303 然后我设置 mapView 参数(包括 showUserLocation)并在第二个获取以下值呼叫 - 纬度:2.5498006923989488e-301,经度:1.2256803803597691e-282 ...
【解决方案2】:

让我澄清我的问题...... 据我了解,userLocation 在模拟器上似乎为零......好吧......但第二个问题是为什么不调用 CLLocationManagerDelegate 方法“didUpdateToLocation”? 我看到了这个教程,即使在模拟器上也应该调用它...... 那我做错了什么? http://www.youtube.com/watch?v=qNMNRAbIDoU&feature=related 同时我在日志中收到这样的错误 - “服务器不接受客户端注册 68” 这可能是不调用位置委托的原因吗?

更新:

我在 xCode 4.3 上尝试过,但仍然无法正常工作... 第一次调用 userLocation 它在第二次返回 (0,0) - (-180,-180)

然后我切换到 IOS 5 SDK,现在地图根本没有加载错误 - __GEORegisterNetworkDefaults_block_invoke_0: Could not connect to geod on com.apple.geod.defaults

更新:

卸载所有 xCode 版本,然后安装稳定的 4.2 版本确实有帮助... 已解决!!!

【讨论】:

  • 很好的信息。顺便说一句,如果您最终要安装 Xcode 4.3 beta,请务必将其安装在与 4.2 安装不同的单独目录中。
猜你喜欢
  • 2019-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多