Monday, October 10, 2011

Getting GPS coordinates and compass heading in an iOS FireMonkey app

GPS and compass in FireMonkey

Below is a code snippet that shows how to write the delegates that get GPS and Compass heading information from the device.

Don't worry - cleaned up complete examples are forthcoming shortly.
{$IFDEF FPC}
type
MyCLController = objcclass(NSObject)
locationManager : CLLocationManager;
procedure locationManager_didUpdateToLocation_fromLocation(manager: CLLocationManager; newLocation, oldLocation: CLLocation); message 'locationManager:didUpdateToLocation:fromLocation:';
procedure locationManager_didUpdateHeading(manager: CLLocationManager; newHeading: CLHeading); message 'locationManager:didUpdateHeading:';
end;

var
Controller : MyCLController;
{$ENDIF}

{$IFDEF FPC}
procedure MyCLController.locationManager_didUpdateToLocation_fromLocation(manager: CLLocationManager; newLocation, oldLocation: CLLocation);
begin
// Do stuff with newLocation.coordinate.latitude and .longitude here
end;
{$ENDIF}

{$IFDEF FPC}
procedure MyCLController.locationManager_didUpdateHeading(manager: CLLocationManager; newHeading: CLHeading);
begin
// Do stuff with newHeading.magneticheading here
end;
{$ENDIF}

procedure TForm1.FormCreate(Sender: TObject);
begin
{$IFDEF FPC}
Controller := MyCLController.alloc.init;
Controller.locationManager := CLLocationManager.alloc.init;
Controller.locationManager.setDelegate(Controller);
Controller.locationManager.startUpdatingLocation;
Controller.locationManager.startUpdatingHeading;
{$ENDIF}
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{$IFDEF FPC}
Controller.locationManager.release;
Controller.release;
{$ENDIF}
end;

12 comments:

  1. Anders,

    Outstanding job with all of these iOS examples. I expect that they will be very helpful. Thanks.

    ReplyDelete
  2. Hi,

    Are you sure this doesn't require you to parse the additional header files for the CoreLocation framework ? I have been trying to use the CLLocationManager from within Delphi, but never succeeded. It should be possible by parsing the additional header files for the required frameworks, but alas ... I never succeeded in doing that ...


    Regards,


    Stefaan

    ReplyDelete
  3. It does, in fact, require that you parse the CoreLocation headers. I will provide an example of this soon.

    ReplyDelete
  4. I find your iOS FireMonkey blog entries very interesting, and educational. Keep them coming.

    One issue though - are you able to fix the formatting of the code (add syntax highlighting)? In the current state, it is quie difficult to read.

    ReplyDelete
  5. Well ... once I know how to parse / build the other iOS FrameWorks, I will surrely make another Delphi XE2 / FireMonkey tutorial on that topic.

    ReplyDelete
  6. Anders,

    Thank you so much for sharing these nice tips and tricks. I can't wait getting started with Firemonkey.

    You should probably fix the orientation of the compass-dial - it is pointing towards 29 degrees - not 331 ;o)

    best regards
    Jan

    ReplyDelete
  7. ehh - unless the dot is north.....
    Sorry....

    ReplyDelete
  8. I was able to find good information from your blog posts.

    ReplyDelete
  9. My pal advisable I may such as this internet site. He or she once was entirely proper.. Day4deal Every Day The Best Deals This organize truly made our day. You actually can not consider simply just how a whole lot time frame I needed used just for this information and facts! Thank you!

    ReplyDelete
  10. Is there a service available that geotags blog posts while on the go and is able to post to map with others?

    ReplyDelete
  11. Supercharge your marketing campaign with our social media promotion services. Buy high quality likes/followers/views at highly competitive prices and with fast delivery.

    Get 5,000 Twitter Followers from:- http://khalaghor.com/twitter.html


    You can also order smaller or bigger package from our official website.

    ReplyDelete

Note: Only a member of this blog may post a comment.