iPhone当前用户位置坐标显示为(0,0) [英] iPhone current user location coordinates showing as (0,0)

查看:165
本文介绍了iPhone当前用户位置坐标显示为(0,0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此viewDidLoad方法获取用户当前的纬度和经度。生成的地图正确指示当前位置,但NSLog始终显示:

I'm trying to get the users current latitude and longitude with this viewDidLoad method. The resulting map is correctly indicating the current location however the NSLog consistently shows:

2009-09-19 16:45:29.765 Mapper[671:207] user latitude = 0.000000
2009-09-19 16:45:29.772 Mapper[671:207] user longitude = 0.000000

任何人都知道我在这里缺少什么?在此先感谢您的帮助!

Anyone know what I am missing here? Thanks in advance for your help!

- (void)viewDidLoad {
    [super viewDidLoad];
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setShowsUserLocation:YES];

    CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);
}


推荐答案

地图不会尝试据我所知,获取用户位置直到它实际显示。对此的一个很好的测试是,你何时会得到一个对话框,询问你是否可以让应用获得用户位置?根据我的经验,只有在地图显示在屏幕上之后才会出现。

The map will not try to get the users location until it's actually displayed, as far as I know. A good test of that is, when do you get the dialog asking you if it's OK for the app to get the users location? In my experience that has always come up only after the map shows on screen.

如果你必须在启动时有一个位置,解决这个问题的最佳方法是实现经典的核心位置处理程序代码并从最初获取位置。一旦地图启动,您就可以停止从那里接收更新并从那里获得进一步的更改(尽管如果您需要不断更新,最好只保留标准的核心位置更新。如果您考虑它,当您有显示用户时位置在地图中启用您的应用程序进一步使用核心位置基本上是免费的,因为无论如何GPS都会被启动。

The best way to approach this problem if you must have a location on startup, is to implement the classic Core Location handler code and get the location from that initially. You can stop receiving updates from there once the map is up and get further changes from there (though if you need constant updates you are better off just keeping with the standard Core Location updates. If you think about it, when you have "shows user location" enabled in the map further use of Core Location by your app is essentially free since the GPS will be fired up the whole time anyway.

这篇关于iPhone当前用户位置坐标显示为(0,0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆