0

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

4

2 回答 2

0

改变 :

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

现在 latitude 和 longitude 应该作为floatnot获得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

编辑:请参阅链接。

于 2012-11-17T06:55:02.567 回答
0

请下载此文件

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

LocationGetter.h 和 .m

物理位置.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];
}

你得到你所有的整洁的纬度,液化天然气,当前城市,国家,你需要的一切。希望它可以帮助你。 注意:-模拟器给出不正确的结果,您必须在设备上进行测试。

更新

更新DEMO

于 2012-11-17T07:02:05.493 回答