distanceFromLocation - Calculate distance between two points

后端 未结 7 969
礼貌的吻别
礼貌的吻别 2020-11-29 21:27

Just a quick question on Core Location, I\'m trying to calculate the distance between two points, code is below:

    -(void)locationChange:(CLLocation *)newL         


        
7条回答
  •  有刺的猬
    2020-11-29 21:34

    The problem here is that you're calling an object method:

    - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
    

    of class CLLocation.

    CLLocationCoordinate2D is in fact a structure, consisting of two doubles:

    typedef struct
    {
        CLLocationDegrees latitude;
        CLLocationDegrees longitude;
    } CLLocationCoordinate2D;
    

    Proper way of doing this is to get a CLLocation object and call distanceFromLocation on it. Like this:

    CLLocation* newLocation;
    CLLocation* oldLocation;
    CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
    

    Of course you first need to initialize both of those values (from CLLocationManager, for instance).

提交回复
热议问题