Delphi for iOS开发指南(12):在iOS Device中使用地理定位

Delphi for iOS开发指南(12):在iOS Device中使用地理定位

 


在开始这篇教程之前,你应该预先阅读并按下面的这篇教程实际操作过:

  • iOS开发指南(9)::在iOS应用程序中使用ListBox来显示TableView
  • iOS开发指南(7)::在iOS应用程序中使用WebBrowser
  • iOS开发指南(10)::在iOS应用程序中使用Layout来适应不同的窗体大小和窗体排列方向

这篇教程讲述了定位你的iOS Device的基本步骤(使用经纬度),并使用Reverse Geocoding(反向地址编码)来转换成可以阅读的地址,如下图所示:

 

 

 

 

 

 

设计用户界面

 


这个示例程序使用两个主要的组件设计而成:一个TListBox(在左边)和一个TWebBrowser。


在ListBox中,设置Align属性为alLeft来保留UI的整个左边为它所用。然后在ListBox上创建下面这些子控件:

  • 一个TListBoxHeader控件,它有下列两个子控件:

                 一个显示标题为“Location Demo”的TLabel组件
                 一个开关控件来选择开启/关闭TLocationSensor

  • 一个文本为“Your Location”的TListBoxGroupHeader
            一个TListBoxItem,它的Name为“ListBoxItemLatitude”,Text为“Latitude”
            一个TListBoxItem,它的Name为“ListBoxItemLongitude”,Text为“Longitude”
            一个TListBoxGroupHeader,Text为“Current Address”
            一个TListBoxItem,Name为“ListBoxItemAdminArea”,Text为“AdminArea”
            一个TListBoxItem,Name为“ListBoxItemCountryCode”,Text为“CountryCode”
            一个TListBoxItem,Name为“ListBoxItemCountryName”,Text为“CountryName”
            一个TListBoxItem,Name为“ListBoxItemFeatureName”,Text为“FeatureName”
            一个TListBoxItem,Name为“ListBoxItemLocality”,Text为“Locality”
            一个TListBoxItem,Name为“ListBoxItemPostalCode”,Text为“PostalCode”
            一个TListBoxItem,Name为“ListBoxItemSubAdminArea”,Text为“SubAdminArea”
            一个TListBoxItem,Name为“ListBoxItemSubLocality”,Text为“SubLoality”
            一个TListBoxItem,Name为“ListBoxItemSubThoroughfare”,Text为“SubThoroughfare”
            一个TListBoxItem,Name为“ListBoxItemThoroughfare”,Text为“Thoroughfare”
            一个TWebBrowser组件(WebBrowser1)来显示网页(Google Maps)。设置Align属性为alClient。

 

 

在你创建完这些组件之后,选择所有的TListBoxItem项目,然后在StyleLookup属性中选择为listboxitemleftdetail。这允许TListBoxItem来同时显示一个Label和详细信息文本。

 

 

 


 

 

 

 

 

 

 


位置传感器

 


位置传感器由TLocationSensor组件封装。

 

 

TLocationSensor触发一个OnLocationChanged事件,当Device检测到移动时。你可以使用Distance属性来调整TLocationSensor的灵敏度。如果你设置Distance为“10”,当你移动了“10”米时,TLocationSensor就会触发OnLocationChanged事件。

 

 

 

 

 

 

 


从LocationSensor组件获取位置信息(经纬度)

 


首先,TLocationSensor组件需要激活才能使用。可以基于你的输入来开启/关闭TLocationSensor,例如使用一个TSwitch组件,或其他应用程序事件。

 


这里有一段代码,它根据TSwitch组件值的更改来控制TLocationSensor:

 


 

procedure TForm44.Switch1Switch(Sender: TObject);

begin

  LocationSensor1.Active := Switch1.IsChecked;

end;


 

 


就像之前提到过的,当你移动iOS Device时TLocationSensor就会触发一个OnLocationChanged事件。你可以在这个事件处理过程中使用它的参数来显示当前的位置,方法如下:

 


 

procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;

  const OldLocation, NewLocation: TLocationCoord2D);

begin

  // Show current location

  ListBoxItemLatitude.ItemData.Detail  := NewLocation.Latitude.ToString;

  ListBoxItemLongitude.ItemData.Detail := NewLocation.Longitude.ToString;

end;


 

 

 

 

 

 

 

 

 

 


 

通过TWebBrowser组件使用Google Maps来显示当前的位置

 


之前在“iOS教程:在iOS应用程序中使用TWebBrowser组件”这篇教程中讲到过的,TWebBrowser组件封装了iOS的WebBrowser。

 


你可以从TWebBrowser组件来调用Google Maps,使用如下URL参数:

 

https://maps.google.com/maps?q=(Latitude-value),(Longitude- value)&output=embed 


 

 


因此你可以添加这个URL到你之前创建的OnLocationChanged事件处理过程中,如下:

 

procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;

  const OldLocation, NewLocation: TLocationCoord2D);

var

  URLString: String;

begin

  // code for previous step goes here

 

  // Show Map using Google Maps

  URLString := Format(

    'https://maps.google.com/maps?q=%s,%s&output=embed',

      [NewLocation.Latitude.ToString, NewLocation.Longitude.ToString]);

  WebBrowser1.Navigate(URLString);

end;


 

 

 

 

 

 

 

 

使用反向地理编码

 


TGeocoder是一个封装了地理编码(或反向地理编码)服务的对象。

 


地理编码是翻译地理数据的过程,例如地址和邮编,转换成地理坐标。反向地理编码是将地地理坐标转换成地理数据的过程,例如地址:

 


在我们这个例子中,我们使用TGeocoder将我们的位置(以经纬度的形式)“反向地理编码”成可读的地址信息。

 

 

 

这里是使用TGeocoder的基本操作步骤:

 

1. 创建一个TGeocoder的实例。
2.定义一个OnGeocodeReverse事件,以便你之后能够接收到这个事件。
3.设置数据来执行“反向地址编码”。
4.TGeocoder访问网络上的服务来处理地址信息。
5. TGeocoder触发一个OnGeocodeReverse事件。
6.你的iOS应用通过OnGeocodeReverse事件的参数来接收地址信息,然后更新用户界面。

 


因为TGeocoder不是一个组件(它只是一个类),你需要通过你的代码来定义这些步骤(你不能拖放一个组件,也不能通过Object Inspector来赋一个事件处理过程)。

 


首先,在窗体的Private声明区域定义一个新的成员“FGeocoder:TGeocoder”。你也可以照着下面这段代码来定义一个“OnGeocoderReverseEvent过程”。

 

 

 
type

  TForm44 = class(TForm)

    // IDE defines visible (or non-visual) components here automatically

  private

    { Private declarations }

    FGeocoder: TGeocoder;

    procedure OnGeocodeReverseEvent(const Address: TCivicAddress);

  public

    { Public declarations }

  end;

 

 


当你定义了这2行代码,将光标定位到OnGeocodeReverseEvent,然后按Ctrl+Shift+C,这会在你的代码中创建如下过程(之后你会使用到的):

 

procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);

begin

 

end;


 

 

 


现在你可以创建一个TGeocoder的实例,并使用下列代码来初始数据。

 


TGeocoder.Current提供了实际实现Geocoding服务的类类型,“TGeocoder.Current.Create”调用指定类类型的构造方法,然后保存到FGeocoder成员。你也需要指定一个事件处理过程,它在TGeocoder完成反向地理编码时触发。将OnGeocodeReverseEvent(你之前那一步刚刚定义的)赋给FGeocoder.OnGeocodeReverse。

 


最后,如果你成功创建了一个TGeocoder的实例,并且TGeocoder没有运行,使用地址信息调用TGeocoder.GeocodeReverse。当TGeocoder接收到数据之后,OnGeocoderReverseEvent事件就触发了。

 

procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;

  const OldLocation, NewLocation: TLocationCoord2D);

begin

  // code for previous steps goes here

 

  // Setup an instance of TGeocoder

  if not Assigned(FGeocoder) then

  begin

    if Assigned(TGeocoder.Current) then

      FGeocoder := TGeocoder.Current.Create;

    if Assigned(FGeocoder) then

      FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;

  end;

 

  // Translate location to address

  if Assigned(FGeocoder) and not FGeocoder.Geocoding then

    FGeocoder.GeocodeReverse(NewLocation);

end;


 

 

 

 

 

在ListBox组件中显示一个可读的地址

 


之前提到过的,在反向地理编码完成之后,一个OnGeocodeReverseEvent会触发。

 


接下来,将TCivicAddress地址参数中的属性赋给在ListBox中的字段:

 


 

procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);

begin

  ListBoxItemAdminArea.ItemData.Detail       := Address.AdminArea;

  ListBoxItemCountryCode.ItemData.Detail     := Address.CountryCode;

  ListBoxItemCountryName.ItemData.Detail     := Address.CountryName;

  ListBoxItemFeatureName.ItemData.Detail     := Address.FeatureName;

  ListBoxItemLocality.ItemData.Detail        := Address.Locality;

  ListBoxItemPostalCode.ItemData.Detail      := Address.PostalCode;

  ListBoxItemSubAdminArea.ItemData.Detail    := Address.SubAdminArea;

  ListBoxItemSubLocality.ItemData.Detail     := Address.SubLocality;

  ListBoxItemSubThoroughfare.ItemData.Detail := Address.SubThoroughfare;

  ListBoxItemThoroughfare.ItemData.Detail    := Address.Thoroughfare;

end;


 

 

 

 

 

 


 

 翻译的不好,请大家贱谅!

欢迎加入OrangeUI For FMX 技术支持QQ群10900297

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值