简体   繁体   中英

how to get the current location in latitude and longitude in objective c

I am using the following code to get the current location,I have added the corelocation framework

- (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;
}

But i am getting the default values ie, Latitude : 42° 17' 36.8898" and Longitude : -71° 27' 43.1003" where my current Latitude:N 12° 56' 11.5624" and Longitude:E 77° 32' 41.0834". How to get the exact latitude and longitude. where i am doing the mistake or do i need to add any other method or function or framework.

Thanks in Advance.

You can add this method where you need to get the current latitude and longitude:

-(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;
}

And to call it use:

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

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

Test the application on real device. The Simulator will always show you the default value for Latitude and Longitude. Also make sure that the location services are enabled on your device.

You have to test location service in device, not simulator, though you can enable location simulation on simulator, see my image attachment, the circled icon. But it won't be your exact location! But at least you can verify your code is working 图片

您需要在设备上测试它以获取当前位置的纬度和经度,我猜您正在获得Cupertino的纬度和经度,这是默认值。

First You should enable Location Service in your Simulator. Debug -> Location. Most of the case you will be getting lat and long values of Cupertino. And you can set Custom locations for this option by selecting the second option(Custom Location). These option will automatically triggered when we are selecting location when runtime in Debugging area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM