开发者

I can't get the gps coordinates for a persistent period of time

I have implemented the standard method of retrieving the coordinates from the gps using - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation.

The problem is that this function is called only on initialization and not throughout the life of the program. Is this normal?

In android you would implement a listener and you would get data instantly.

Is this not the wright way how i'm doing it? If it is, what could be the problem? (btw i've checked, i don't 开发者_C百科stopUpdatingLocation)

I have a CLLocationManager inherited class named testing and initialize it

testing* cllm = [[testing alloc] init];
        cllm.delegate = self;

i later start the updating

[cllm startUpdatingLocation];
self.locationManagerDelegate = delegate;

and later is called

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

but after that it isn't called anymore. I need it to be called frequently so that i may calculate the distance to a certain point X from where i am.


Agreed with @Matt, without more code the best solution I can offer is this to tell it to update every time the device is moved with:

[self.locationManager setDistanceFiler:kCLDistanceFilterNone]

Update

I went through past projects and found the code I believe you are looking for assuming your location manager subclass is working properly

- (void)viewDidLoad {
[super viewDidLoad];
//Location

// create new location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;

// start location manager
[self.locationManager startUpdatingLocation];
}

-(void) distanceBetweenUserandPin {
CLLocation *currentUserLocation = [[CLLocation alloc] initWithLatitude:_currentLocation.latitude longitude:_currentLocation.longitude];
CLLocation *currentPinLocation = [[CLLocation alloc] initWithLatitude:_pinLocation.latitude longitude:_pinLocation.longitude];
CLLocationDistance distanceBetweenUserAndPinMeters = [currentUserLocation distanceFromLocation:currentPinLocation];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//This successfully saves Lat, Long Data to a point location
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(@"%f, %f", location.latitude, location.longitude);
//This assigns the value of location to the ivar _currentLocation
_currentLocation = CLLocationCoordinate2DMake(location.latitude, location.longitude);
NSLog(@"%f, %f", _currentLocation.latitude, _currentLocation.longitude);    

}


First, it seems strange to me that you would use a subclass of CLLocationManager, since I'm not sure what benefit that provides you. Assuming that's not the problem, however...

From the CLLocationManager documentation:

This method returns immediately. Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateToLocation:fromLocation: method. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading.

What's happening is that it is being called once for the initial position fix, but it isn't calling again because other conditions haven't changed. If a user doesn't move anywhere, then new location data won't be provided since it will be the same as last time (with a few exceptions as mentioned in the docs).

When you're testing your app, make sure that you try moving around and changing your location to produce an update. If that doesn't work, try experimenting with the desiredAccuracy and distanceFilter properties:

You start standard location services by calling the startUpdatingLocation method. This service is most appropriate for applications that need more fine-grained control over the delivery of location events. Specifically, it takes into account the values in the desiredAccuracy and distanceFilter property to determine when to deliver new events.

Other than that, I'd guess it might have to do with how you're subclassing CLLocationManager. Providing some of that code might help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜