使用RadStudio 10.4在iOS TMapView上显示用户位置的问题在10中不会发生

问题描述

我正在尝试在具有TMapView对象和TLocationSensor对象的多设备表单上显示用户位置。代码如下。我不太确定是否要同步OnLocationChange事件,因此存在用于线程检查的代码。显然,对于iOS,回调发生在主执行线程中,因此不需要同步,对吗?我要做的就是在地图上显示用户位置(即蓝色发光的小圆圈),但是一旦更新地图,TMapKitDelegate.mapViewViewForAnnotation-> TMapKitMapMarker.AnnotationView

就会发生异常。

这个问题使我发疯,希望有人可以看到我做错的任何愚蠢的事情,尽管我在Embarcadero RadStudio 10.3中没有问题。如果我在10.3中构建了确切的代码并将其部署到我的iPhone上,它将正常工作并在地图上显示使用位置

感谢您的帮助

unit FMain;

interface

uses

  System.SysUtils,System.Types,System.UITypes,System.Classes,System.Variants,FMX.Types,FMX.Controls,FMX.Forms,FMX.Graphics,FMX.Dialogs,FMX.Maps,{FMX.Maps.iOS,}

  System.Sensors,System.Sensors.Components;

type

  TfMainForm = class(TForm)

    tmvMapView: TMapView;

    tlocLocationSensor: TLocationSensor;

    procedure tlocLocationSensorLocationChanged(Sender: TObject; const OldLocation,NewLocation: TLocationCoord2D);

    procedure FormCreate(Sender: TObject);

  private

    { Private declarations }

    mCurrentLocation:  TLocationCoord2D;

    procedure ProcessLocationChange;

  public

    { Public declarations }

  end;

var

  fMainForm: TfMainForm;

implementation

{$R *.fmx}

procedure TfMainForm.tlocLocationSensorLocationChanged(Sender: TObject; const OldLocation,NewLocation: TLocationCoord2D);

var mainThread:     TThreadID;

    currentThread:  TThreadID;

begin

  mCurrentLocation.Latitude  := -37.78001; //NewLocation.Latitude;

  mCurrentLocation.Longitude := 144.98890; //NewLocation.Longitude;

  mainThread := System.MainThreadID;

  currentThread := TThread.Current.ThreadID;

  //  Check to see if we need to do a synchronize or not. Apparently for iOS

  //  the location sensor callback runs in the main thread.

  //

  if mainThread <> currentThread then

    TThread.Synchronize(TThread.Current,ProcessLocationChange)

  else

    ProcessLocationChange;

end;

procedure TfMainForm.FormCreate(Sender: TObject);

begin

  tlocLocationSensor.Active := True;

end;

procedure TfMainForm.ProcessLocationChange;

var mapCenter:  TMapCoordinate;

begin

  //  Center the map view around our current position and set the zoom to 18

  mapCenter := TMapCoordinate.Create(mCurrentLocation.Latitude,mCurrentLocation.Longitude);

  tmvMapView.Location := mapCenter;

  tmvMapView.Zoom := 18;

end;

end.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)