获取xcode中用户的当前地址?

时间:2012-11-17 06:11:10

标签: ios core-location cllocationmanager

我需要获取用户的确切当前地址(国家,州,城市)。所以我去寻找纬度和经度,然后通过使用反向地理编码找出它。但是无法获得经度和经度本身。我使用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。但仍然看不到位置....?是这样吗?

2 个答案:

答案 0 :(得分:0)

更改:

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链接。

答案 1 :(得分:0)

请下载this file

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

LocationGetter.h和.m

PhysicalLocation.h和.m

你需要导入

#import "PhysicalLocation.h"

 PhysicalLocation *physicalLocation = [[PhysicalLocation alloc] init];
    [physicalLocation getPhysicalLocation];
getPhysicalLocation PhysicalLocation.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