Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLLocation speed

I am developing GPS application. Do you know about how to detect speed of mobile device ?

Actually, I need to detect the speed every 2 seconds.

I know didUpdateToLocation method is called when location changed.

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

But I think this method is not suitable for my issue.

So, Do I need to check speed of [CLLocationManager location] in 2 seconds ?

Any suggestion ?

Thanks in advance.

like image 439
Ferdinand Avatar asked Sep 08 '09 01:09

Ferdinand


People also ask

What is CLLocation in Swift?

A CLLocation object contains the geographical location and altitude of a device, along with values indicating the accuracy of those measurements and when they were collected. In iOS, a location object also contains course information — that is, the speed and heading in which the device was moving.

What is meant by Core location?

Core Location provides services that determine a device's geographic location, altitude, and orientation, or its position relative to a nearby iBeacon device. The framework gathers data using all available components on the device, including the Wi-Fi, GPS, Bluetooth, magnetometer, barometer, and cellular hardware.


1 Answers

How about the code below which works from the delegate method. Alternatively, if you did want to poll, then keep your previous location and check the distance changed from the last poll and use the manual method (also shown below) to calculate the speed.

Speed is calculated/provided in m/s so multiply by 3.6 for kmph or 2.23693629 for mph.

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   //simply get the speed provided by the phone from newLocation
    double gpsSpeed = newLocation.speed;

    // alternative manual method
    if(oldLocation != nil)
    {
        CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
        NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
        double calculatedSpeed = distanceChange / sinceLastUpdate;

    }   
}
like image 131
RR. Avatar answered Oct 02 '22 14:10

RR.