如何在iOS中访问指南针?

前端之家收集整理的这篇文章主要介绍了如何在iOS中访问指南针?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Firemonkey中访问 iphone指南针?

解决方法

这是一个howto视频: http://blogs.embarcadero.com/ao/2011/10/13/39171
完整源代码http://cc.embarcadero.com/item/28536
和(短)文章: @L_403_3@

这是罗盘组件的源代码.
还没有尝试过,但它应该工作.

  1. unit Compass;
  2.  
  3. {Based on Anders Ohlsson Firemonkey Compass example}
  4.  
  5. {$IFDEF FPC}
  6. {$mode objfpc}{$H+}
  7. {$modeswitch objectivec1}
  8. {$ENDIF}
  9.  
  10. interface
  11.  
  12. uses
  13. SysUtils,Classes,FMX_Types
  14. {$IFDEF FPC},iPhoneAll
  15. {$ENDIF}
  16. ;
  17.  
  18. type
  19. TUpdateEvent = TNotifyEvent;
  20.  
  21. TiOSGPSCompass = class(TFmxObject)
  22. private
  23. FLatitude: Double;
  24. FLongitude: Double;
  25. FAltitude: Double;
  26. FHeading: Double;
  27. FOnUpdate: TUpdateEvent;
  28. UpdateNeeded: Boolean;
  29. protected
  30. procedure SetLatitude(value: Double);
  31. procedure SetLongitude(value: Double);
  32. procedure SetAltitude(value: Double);
  33. procedure SetHeading(value: Double);
  34. procedure Update;
  35. public
  36. constructor Create(AOwner: Classes.TComponent); override;
  37. destructor Destroy; override;
  38. published
  39. property Latitude: Double read FLatitude;
  40. property Longitude: Double read FLongitude;
  41. property Altitude: Double read FAltitude;
  42. property Heading: Double read FHeading;
  43. property OnUpdate: TUpdateEvent read FOnUpdate write FOnUpdate;
  44. end;
  45.  
  46. var
  47. MyGPSCompass: TiOSGPSCompass = nil;
  48.  
  49. procedure Register;
  50.  
  51. implementation
  52.  
  53. {$IFDEF FPC}
  54. uses
  55. CoreLocation;
  56. {$ENDIF}
  57.  
  58. {$IFDEF FPC}
  59. type
  60. MyCLController = objcclass(NSObject)
  61. locationManager : CLLocationManager;
  62. procedure locationManager_didUpdateToLocation_fromLocation(manager: CLLocationManager; newLocation,oldLocation: CLLocation); message 'locationManager:didUpdateToLocation:fromLocation:';
  63. procedure locationManager_didUpdateHeading(manager: CLLocationManager; newHeading: CLHeading); message 'locationManager:didUpdateHeading:';
  64. end;
  65.  
  66. var
  67. Controller : MyCLController;
  68. {$ENDIF}
  69.  
  70.  
  71. {$IFDEF FPC}
  72. procedure MyCLController.locationManager_didUpdateToLocation_fromLocation(manager: CLLocationManager; newLocation,oldLocation: CLLocation);
  73. begin
  74. if Assigned(MyGPSCompass) then begin
  75. MyGPSCompass.SetLatitude(newLocation.coordinate.latitude);
  76. MyGPSCompass.SetLongitude(newLocation.coordinate.longitude);
  77. MyGPSCompass.SetAltitude(newLocation.altitude);
  78. MyGPSCompass.Update;
  79. end;
  80. end;
  81. {$ENDIF}
  82.  
  83. {$IFDEF FPC}
  84. procedure MyCLController.locationManager_didUpdateHeading(manager: CLLocationManager; newHeading: CLHeading);
  85. begin
  86. if Assigned(MyGPSCompass) then begin
  87. MyGPSCompass.FCompassHeading:= newHeading.magneticheading;
  88. MyGPSCompass.Update;
  89. end;
  90. end;
  91. {$ENDIF}
  92.  
  93. constructor TiOSGPSCompass.Create(AOwner: TComponent);
  94. begin
  95. inherited;
  96. {$IFDEF FPC}
  97. Controller := MyCLController.alloc.init;
  98. Controller.locationManager := CLLocationManager.alloc.init;
  99. Controller.locationManager.setDelegate(Controller);
  100. Controller.locationManager.setDesiredAccuracy(kCLLocationAccuracyBestForNavigation);
  101. Controller.locationManager.startUpdatingLocation;
  102. Controller.locationManager.startUpdatingHeading;
  103. {$ENDIF}
  104. MyGPSCompass:= Self;
  105. end;
  106.  
  107. destructor TiOSGPSCompass.Destroy;
  108. begin
  109. {$IFDEF FPC}
  110. Controller.locationManager.release;
  111. Controller.release;
  112. {$ENDIF}
  113. inherited;
  114. end;
  115.  
  116. procedure TiOSGPSCompass.SetLatitude(value: Double);
  117. begin
  118. if (FLatitude <> value) then begin
  119. FLatitude:= value;
  120. UpdateNeeded:= True;
  121. end;
  122. end;
  123.  
  124. procedure TiOSGPSCompass.SetLongitude(value: Double);
  125. begin
  126. if (FLongitude <> value) then begin
  127. FLongitude:= value;
  128. UpdateNeeded:= True;
  129. end;
  130. end;
  131.  
  132. procedure TiOSGPSCompass.SetAltitude(value: Double);
  133. begin
  134. if (FAltitude <> value) then begin
  135. FAltitude:= value;
  136. UpdateNeeded:= True;
  137. end;
  138. end;
  139.  
  140. procedure TiOSGPSCompass.SetHeading(value: Double);
  141. begin
  142. if (FHeading <> value) then begin
  143. FHeading:= value;
  144. UpdateNeeded:= True;
  145. end;
  146. end;
  147.  
  148. procedure TiOSGPSCompass.Update;
  149. begin
  150. if (UpdateNeeded and Assigned(OnUpdate)) then OnUpdate(Self);
  151. UpdateNeeded:= False;
  152. end;
  153.  
  154. procedure Register;
  155. begin
  156. RegisterComponents('iOS',[TiOSGPSCompass]);
  157. end;
  158.  
  159. end.

猜你在找的iOS相关文章