Writing back to CLLocation object?

Discussion in 'iOS Programming' started by xArtx, Feb 27, 2013.

  1. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    Hi Guys,
    Pretty much what the title says.
    I can read from a CLLocation into a float value:
    Code:
    latitude = positionone.coordinate.latitude;
    
    but the compiler doesn't want to know about writing back
    to an object I declared myself:
    Code:
    positionone.coordinate.latitude = latitude;
    
    I found that odd. Is there another way to do it?
    Cheers, Art.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    CLLocation is an NSObject. CLLocationCoordinate2D is a struct. So this really represents why Apple choosing dot (.) for it's syntactic sugar was a bad idea as what is really being called here is:

    Code:
    [positionone setCoordinate.latitude:latitude];
    
    as the first dot gets expanded/replaced with the accessor. It shoudl be clear that the above is absolute nonsense hence the compiler complaining. This should work:

    Code:
    CLLocationCoordinate2D coord = positionone.coordinate;
    coord.latitude=latitude;
    positionone.coordinate = coord;
    
     
  3. xArtx, Feb 27, 2013
    Last edited: Feb 27, 2013

    xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    Thanks, Apple can have their two plus signs back, I really just want them
    to calculate all my distances between arbitrary coordinates for me :)

    Bah!
    It won't allow the third line. This time it knows it's a read only property.
    It might mean the harder way.... haven't implemented the great circle formula before, but i doesn't look too difficult.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Have you looked into CLLocation's distanceFromLocation: instance method?
     
  5. xArtx, Feb 27, 2013
    Last edited: Feb 27, 2013

    xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #5
    That's what I wanted to use, but you have to be able to write coordinates to
    a CLLocation object first (in order to calc the distance between arbitrary points).
    It's a cinch if you are using it between points the device has actually been to.

    Didn't take long to find a drop in solution (in Km)
    Code:
        //calculate haversine distance for linear distance Km
        double lat1 = screenbrlat;
        double long1 = screentllon;
        double lat2 = screenbrlat;
        double long2 = screenbrlon;
        double dlong = (long2 - long1) * d2r;
        double dlat = (lat2 - lat1) * d2r;
        double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2);
        double c = 2 * atan2(sqrt(a), sqrt(1-a));
        double d = 6367 * c;
        float distance = d;  
    
    Edit,,, In this case, instead of drawing the usual ruler to a GPS screen,
    I want to always display the total width of the screen in Kilometers.
    The program already knows the real geographic coordinates of each corner of the screen.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Here's a quick version using CLLocation:
    Code:
    CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
    CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];;
    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
     
  7. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #7
    Lol.. Bummer :D ...I might as well check for consistency!

    I just learned that the ruler at the bottom of the screen is rather useless
    if the view shows a whole continent.
    As long as the grid is square, the distance measured across the screen
    at one point on the y axis is not going to be the same on another.
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    :confused: Not sure what you mean by this.
     
  9. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #9
    Give me a sec...
     
  10. xArtx, Feb 27, 2013
    Last edited: Feb 27, 2013

    xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #10
    Sorry, internet is almost back to dial up for exceeding bandwidth.
    Anyway,

    [​IMG]

    I totally got that answer wrong...
    I might as well check that the output of the C routine posted above
    matches the output of the native code you posted.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Understood. Now I'm curious: If you find the output doesn't match, which one are you going to trust and how would you decide that?
     
  12. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #12
    Measure the distance of one degree on a grid.
    We can get a mean radius from a mean circumference which is defined in the formula.

    ----------

    The formula doesnt account for the Earth's slight eliptiod (I don't think). Now I'm no longer thinking
    I can just measure a degree longitude at 45deg South, and multiply it to see if the mean circumference matches.

    ----------

    The formula doesnt account for the Earth's slight eliptiod (I don't think) But the defined mean circumference might. Now I'm no longer thinking
    I can just measure a degree longitude at 45deg South, and multiply it to see if the mean circumference matches.
     
  13. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #13
    Hey, I followed through :)

    Not sure I explained myself well there.
    In a nutshell, measure what each formula thinks the circumference of
    the Earth is at a given degree of latitude, and compare with a known one
    (doing it by measuring one grid and multiplying it).

    I think I'd rather just keep the C one because I can access every component of it.
    If I move to Mars for example, there's only one small change to make,
    and don't have to wait for an iOS update.

    They do disagree slightly, but I do think that's due to a different definition
    of mean circumference of the Earth.
    If you wanted to be anal, you could increment it in the C version
    to see if it ever agreed all of the time.

    Measurement across the screen for the picture posted above,
    Australia must be in a different lat postion on the screen,
    this time it's 4320.7Km across the middle versus the native code 4333.9Km.
     

Share This Page