【问题标题】:how to get the current location in latitude and longitude in objective c如何在目标c中获取经纬度的当前位置
【发布时间】:2012-02-18 09:24:14
【问题描述】:

我正在使用以下代码获取当前位置,我已经添加了核心定位框架

- (void)viewDidLoad {
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    int degrees = newLocation.coordinate.latitude;
    double decimal = fabs(newLocation.coordinate.latitude - degrees);
    int minutes = decimal * 60;
    double seconds = decimal * 3600 - minutes * 60;
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                     degrees, minutes, seconds];
    NSLog(@" Current Latitude : %@",lat);
    //latLabel.text = lat;
    degrees = newLocation.coordinate.longitude;
    decimal = fabs(newLocation.coordinate.longitude - degrees);
    minutes = decimal * 60;
    seconds = decimal * 3600 - minutes * 60;
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
    NSLog(@" Current Longitude : %@",longt);
    //longLabel.text = longt;
}

但我得到的是默认值,即纬度:42° 17' 36.8898" 和经度:-71° 27' 43.1003" 我当前的纬度:N 12° 56' 11.5624" 和经度:E 77° 32' 41.0834"。如何获得准确的纬度和经度。 我在哪里做错了,或者我需要添加任何其他方法或功能或框架。

提前致谢。

【问题讨论】:

  • 你是在真机还是模拟器上测试的?
  • 您是否在设备上启用了定位服务?
  • @XSlash 我在模拟器上测试过。
  • @H2CO3 我没有检查设备
  • 您是否在 XCode 上启用了位置模拟器?

标签: iphone objective-c core-location latitude-longitude


【解决方案1】:

你可以在需要获取当前经纬度的地方加上这个方法:

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

并称之为使用:

CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

NSLog(@"*dLatitude : %@", latitude);
NSLog(@"*dLongitude : %@",longitude);

【讨论】:

  • 当我尝试直接访问它们时,coordinates 是(null)的奇怪行为。印象中,如果代码有一段时间,坐标就会被传递。似乎是时间问题。
  • 这将为纬度和经度返回 0.000000。我正在使用 Xcode 6.1.1 和 iPhone 5s 模拟器。为什么会这样?
  • 非常感谢您发布此答案。对初学者真的很有帮助。
  • 每次都会返回 0.000000 的纬度和经度
【解决方案2】:

在真实设备上测试应用程序。模拟器将始终向您显示纬度和经度的默认值。还要确保在您的设备上启用了定位服务。

【讨论】:

  • 即使在模拟器上也不成问题。在调试区域内,您可以在调试按钮旁边看到,您可以选择不同的区域。您可以选择一些默认位置进行测试。或者你自己创造!
【解决方案3】:

您必须在设备中测试定位服务,而不是模拟器,虽然您可以在模拟器上启用定位模拟,请参阅我的图片附件,带圆圈的图标。但这不会是您的确切位置!但至少您可以验证您的代码是否正常工作

【讨论】:

  • 运行你的应用,它就会出现
  • @X Slash,在运行我的应用程序后,我只检查了它,但它没有像你显示的那样显示任何图标。
  • @shasha Go to product->Edit Scheme->Options->select Allow Location simulation 然后你会得到 X 斜线提到的图标,即使你启用了你赢了't get your 纬度和经度
【解决方案4】:

您需要在设备上对其进行测试以获取您当前位置的经纬度,我猜您正在获取库比蒂诺的默认纬度和经度。

【讨论】:

    【解决方案5】:

    首先,您应该在模拟器中启用定位服务。调试 -> 位置。大多数情况下,您将获得 Cupertino 的 lat 和 long 值。您可以通过选择第二个选项(自定义位置)为此选项设置自定义位置。当我们在调试区域运行时选择位置时,这些选项将自动触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2011-08-31
      • 2017-05-09
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多