简体   繁体   中英

Not getting appropriate Latitude Longitude

I am a iOS application developer. I am working with Location Manager.

For every 5 seconds I am capturing the latitude and longitude and storing the CLLocation object in array. The lat and long is not completely correct when I am trying to draw those location in Map.

My code is following below:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{//ios6&+
CLLocation *loc;
NSInteger objCount = [locations count];
if (objCount >= 1) {
    loc = [locations objectAtIndex:objCount - 1];

    NSArray *Arr_TotalTime = [Str_TotalTime componentsSeparatedByString:@":"];

            [ArrayOfLocations addObject:loc];
    }
}

Any one can please give me more info to get ACTUAL location (lat, long) from didupdatelocation method.

Any help will be highly appreciated.

Depending on how you set up your location manager, the accuracy will vary. Try something like this:

Location Manager

_locationManager = [[CLLocationManager alloc] init];    
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.activityType = CLActivityTypeAutomotiveNavigation;
_locationManager.distanceFilter = kCLDistanceFilterNone;

Method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];

    NSDate *eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    if ((abs(howRecent) <= 5) && (newLocation.horizontalAccuracy > 0) && (newLocation.verticalAccuracy > 0) && (newLocation.horizontalAccuracy < 100) && (newLocation.verticalAccuracy < 100)) 
    {
        [ArrayOfLocations addObject:newLocation];
    }
    else
    {
        // received bad signal
    }

}

You can use CoreLocation to get the longitude and latitude.

Include framework:

Click your project in navigator.

Click the plus button under "link binary with libraries"

Add Corelocation to your project.

Import the header file:

import

Declare CLLocationManager:

CLLocationManager *locationManager; initialize locationManager:

- (void)viewDidLoad
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
}

Then, use

float latitude = locationManager.location.coordinate.latitude; float longitude = locationManager.location.coordinate.longitude;

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