【问题标题】:How do I access the compass in iOS?如何在 iOS 中访问指南针?
【发布时间】:2011-11-07 21:56:09
【问题描述】:

如何在 Firemonkey 中访问 iphone 指南针?

【问题讨论】:

  • 我不明白,这就像:我问,我给出答案?
  • @RBA,只是分享我的书签。希望它有用。 SO 确实允许这样做,请参阅常见问题解答。
  • 确实:) 你完全正确,这就是给你+1的原因。恕我直言,这也应该放在维基上,因为这是一个关于 iOS 开发的好问题

标签: ios delphi delphi-xe2 firemonkey


【解决方案1】:

这是一个操作视频:http://blogs.embarcadero.com/ao/2011/10/13/39171
完整源代码:http://cc.embarcadero.com/item/28536
还有一篇(短)文章:http://edn.embarcadero.com/article/41715

这里是指南针组件的源代码。
还没有尝试过,但它应该可以工作。

unit Compass;

{Based on Anders Ohlsson Firemonkey Compass example}

{$IFDEF FPC}
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$ENDIF}

interface

uses
  SysUtils, Classes, FMX_Types
{$IFDEF FPC}
  , iPhoneAll
{$ENDIF}
  ;

type
  TUpdateEvent = TNotifyEvent;

  TiOSGPSCompass = class(TFmxObject)
  private
    FLatitude: Double;
    FLongitude: Double;
    FAltitude: Double;
    FHeading: Double;
    FOnUpdate: TUpdateEvent;
    UpdateNeeded: Boolean;
  protected
    procedure SetLatitude(value: Double);
    procedure SetLongitude(value: Double);
    procedure SetAltitude(value: Double);
    procedure SetHeading(value: Double);
    procedure Update;
  public
    constructor Create(AOwner: Classes.TComponent); override;
    destructor Destroy; override;
  published
    property Latitude: Double read FLatitude;
    property Longitude: Double read FLongitude;
    property Altitude: Double read FAltitude;
    property Heading: Double read FHeading;
    property OnUpdate: TUpdateEvent read FOnUpdate write FOnUpdate;
  end;

var
  MyGPSCompass: TiOSGPSCompass = nil;

procedure Register;

implementation

{$IFDEF FPC}
uses
  CoreLocation;
{$ENDIF}

{$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
  if Assigned(MyGPSCompass) then begin
    MyGPSCompass.SetLatitude(newLocation.coordinate.latitude);
    MyGPSCompass.SetLongitude(newLocation.coordinate.longitude);
    MyGPSCompass.SetAltitude(newLocation.altitude);
    MyGPSCompass.Update;
  end;
end;
{$ENDIF}

{$IFDEF FPC}
procedure MyCLController.locationManager_didUpdateHeading(manager: CLLocationManager; newHeading: CLHeading);
begin
  if Assigned(MyGPSCompass) then begin
    MyGPSCompass.FCompassHeading:= newHeading.magneticheading;
    MyGPSCompass.Update;
  end;
end;
{$ENDIF}

constructor TiOSGPSCompass.Create(AOwner: TComponent);
begin
  inherited;
  {$IFDEF FPC}
    Controller := MyCLController.alloc.init;
  Controller.locationManager := CLLocationManager.alloc.init;
    Controller.locationManager.setDelegate(Controller);
  Controller.locationManager.setDesiredAccuracy(kCLLocationAccuracyBestForNavigation);
    Controller.locationManager.startUpdatingLocation;
  Controller.locationManager.startUpdatingHeading;
  {$ENDIF}
  MyGPSCompass:= Self;
end;

destructor TiOSGPSCompass.Destroy;
begin
  {$IFDEF FPC}
  Controller.locationManager.release;
  Controller.release;
  {$ENDIF}
  inherited;
end;

procedure TiOSGPSCompass.SetLatitude(value: Double);
begin
  if (FLatitude <> value) then begin
    FLatitude:= value;
    UpdateNeeded:= True;
  end;
end;

procedure TiOSGPSCompass.SetLongitude(value: Double);
begin
  if (FLongitude <> value) then begin
    FLongitude:= value;
    UpdateNeeded:= True;
  end;
end;

procedure TiOSGPSCompass.SetAltitude(value: Double);
begin
  if (FAltitude <> value) then begin
    FAltitude:= value;
    UpdateNeeded:= True;
  end;
end;

procedure TiOSGPSCompass.SetHeading(value: Double);
begin
  if (FHeading <> value) then begin
    FHeading:= value;
    UpdateNeeded:= True;
  end;
end;

procedure TiOSGPSCompass.Update;
begin
  if (UpdateNeeded and Assigned(OnUpdate)) then OnUpdate(Self);
  UpdateNeeded:= False;
end;

procedure Register;
begin
  RegisterComponents('iOS', [TiOSGPSCompass]);
end;

end.

【讨论】:

  • +1 如果可能的话,最好将相关部分包含在您的答案中。
  • @Smasher,我会看看我能做什么。
  • 很好,这使它成为一个更好的答案。虽然不能做超过我的 +1 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 2011-04-14
  • 1970-01-01
相关资源
最近更新 更多