【问题标题】:get current address of user in xcode?在xcode中获取用户的当前地址?
【发布时间】:2012-11-17 06:11:10
【问题描述】:

我需要获取用户的确切当前地址(国家、州、城市)。所以我去查找经纬度,然后通过使用反向地理编码从中找出。但无法自行获取纬度和经度。我使用xcode 4.1并在iphone模拟器中进行测试。

这是我正在处理的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
    [locationManager startUpdatingLocation];
    NSLog(@"%@", [self deviceLocation]);
}

- (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];
    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];
    longLabel.text = longt;
}

如何找到经纬度从而找到用户的地址?

编辑:

将我的版本更新到 Xcode 4.5。但是还是看不到位置.....?是吗?

【问题讨论】:

  • Xcode 和这个问题真的没有任何关系...

标签: ios core-location cllocationmanager


【解决方案1】:

请下载this file

在我附加的 zip 文件中,有 4 个文件:

LocationGetter.h 和 .m

PhysicalLocation.h 和 .m

你只需要导入

#import "PhysicalLocation.h"

 PhysicalLocation *physicalLocation = [[PhysicalLocation alloc] init];
    [physicalLocation getPhysicalLocation];

getPhysicalLocationPhysicalLocation.m 类中

#pragma mark MKReverseGeocoder Delegate Methods
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{

    NSLog(@"%@",placemark);

    objcntrAppDelegate = (yourappDeleger *)[[UIApplication sharedApplication]delegate];

    objcntrAppDelegate.strCountry=placemark.country;
    objcntrAppDelegate.strSuburb=placemark.subAdministrativeArea;


    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedAddress" object:nil userInfo:nil];


     [geocoder autorelease];
}

您可以得到所有您需要的所有 lat、lng、当前城市、国家/地区。希望它可以帮助你。 注意:- 模拟器给出的结果不正确,您必须在设备上测试。

更新

UpdatedDEMO

【讨论】:

  • 但我发现 MKReverseGeocoder 在 ios5 中已被弃用..我还能用吗?
  • 我可以使用这个并在 ios6 或 ios5 中得到结果,它对我有用,为什么我只是为你发布这个方法
  • 我的小伙伴在同样的大火中工作,得到的结果也是如此
  • k 我已经下载了 zip 文件。现在我应该将它们全部附加到我的项目中吗?
  • 是的,它的 4 个文件添加到您的项目中拖放并检查复制选项,然后在您的应用程序委托中,您只需将文件导入为我的答案并按照我的帖子进行操作
【解决方案2】:

改变:

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

现在应该以float 而不是int 获得纬度和经度。

CLLocation *location = [locationManager location];
// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];

float longitude=coordinate.longitude;
float latitude=coordinate.latitude;

使用CLGeocoder按经纬度查找用户地址。

编辑:参考this链接。

【讨论】:

    猜你喜欢
    • 2012-11-04
    • 2015-03-28
    • 2018-01-10
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多