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

来源:互联网 发布:最新网络p2p在线理财 编辑:程序博客网 时间:2024/05/18 02:34
在开始这篇教程之前,你应该预先阅读并按下面的这篇教程实际操作过:
  • 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:

 


 

[delphi] view plaincopyprint?
  1. procedure TForm44.Switch1Switch(Sender: TObject);  
  2.   
  3. begin  
  4.   
  5.   LocationSensor1.Active := Switch1.IsChecked;  
  6.   
  7. end;  


 

 


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

 


 

[delphi] view plaincopyprint?
  1. procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;  
  2.   
  3.   const OldLocation, NewLocation: TLocationCoord2D);  
  4.   
  5. begin  
  6.   
  7.   // Show current location   
  8.   
  9.   ListBoxItemLatitude.ItemData.Detail  := NewLocation.Latitude.ToString;  
  10.   
  11.   ListBoxItemLongitude.ItemData.Detail := NewLocation.Longitude.ToString;  
  12.   
  13. 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事件处理过程中,如下:

 

[delphi] view plaincopyprint?
  1. procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;  
  2.   
  3.   const OldLocation, NewLocation: TLocationCoord2D);  
  4.   
  5. var  
  6.   
  7.   URLString: String;  
  8.   
  9. begin  
  10.   
  11.   // code for previous step goes here  
  12.   
  13.    
  14.   
  15.   // Show Map using Google Maps  
  16.   
  17.   URLString := Format(  
  18.   
  19.     'https://maps.google.com/maps?q=%s,%s&output=embed',  
  20.   
  21.       [NewLocation.Latitude.ToString, NewLocation.Longitude.ToString]);  
  22.   
  23.   WebBrowser1.Navigate(URLString);  
  24.   
  25. end;  


 

 

 

 

 

 

 

 

使用反向地理编码

 


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

 


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

 


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

 

 

 

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

 

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

 


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

 


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

 

 

[delphi] view plaincopyprint?
  1.    
  2. type  
  3.   
  4.   TForm44 = class(TForm)  
  5.   
  6.     // IDE defines visible (or non-visual) components here automatically  
  7.   
  8.   private  
  9.   
  10.     { Private declarations }  
  11.   
  12.     FGeocoder: TGeocoder;  
  13.   
  14.     procedure OnGeocodeReverseEvent(const Address: TCivicAddress);  
  15.   
  16.   public  
  17.   
  18.     { Public declarations }  
  19.   
  20.   end;  
  21.   
  22.    
  23.   
  24.    


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

 

[delphi] view plaincopyprint?
  1. procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);  
  2.   
  3. begin  
  4.   
  5.    
  6.   
  7. end;  


 

 

 


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

 


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

 


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

 

[delphi] view plaincopyprint?
  1. procedure TForm44.LocationSensor1LocationChanged(Sender: TObject;  
  2.   
  3.   const OldLocation, NewLocation: TLocationCoord2D);  
  4.   
  5. begin  
  6.   
  7.   // code for previous steps goes here  
  8.   
  9.    
  10.   
  11.   // Setup an instance of TGeocoder  
  12.   
  13.   if not Assigned(FGeocoder) then  
  14.   
  15.   begin  
  16.   
  17.     if Assigned(TGeocoder.Current) then  
  18.   
  19.       FGeocoder := TGeocoder.Current.Create;  
  20.   
  21.     if Assigned(FGeocoder) then  
  22.   
  23.       FGeocoder.OnGeocodeReverse := OnGeocodeReverseEvent;  
  24.   
  25.   end;  
  26.   
  27.    
  28.   
  29.   // Translate location to address  
  30.   
  31.   if Assigned(FGeocoder) and not FGeocoder.Geocoding then  
  32.   
  33.     FGeocoder.GeocodeReverse(NewLocation);  
  34.   
  35. end;  


 

 

 

 

 

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

 


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

 


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

 


 

[delphi] view plaincopyprint?
  1. procedure TForm44.OnGeocodeReverseEvent(const Address: TCivicAddress);  
  2.   
  3. begin  
  4.   
  5.   ListBoxItemAdminArea.ItemData.Detail       := Address.AdminArea;  
  6.   
  7.   ListBoxItemCountryCode.ItemData.Detail     := Address.CountryCode;  
  8.   
  9.   ListBoxItemCountryName.ItemData.Detail     := Address.CountryName;  
  10.   
  11.   ListBoxItemFeatureName.ItemData.Detail     := Address.FeatureName;  
  12.   
  13.   ListBoxItemLocality.ItemData.Detail        := Address.Locality;  
  14.   
  15.   ListBoxItemPostalCode.ItemData.Detail      := Address.PostalCode;  
  16.   
  17.   ListBoxItemSubAdminArea.ItemData.Detail    := Address.SubAdminArea;  
  18.   
  19.   ListBoxItemSubLocality.ItemData.Detail     := Address.SubLocality;  
  20.   
  21.   ListBoxItemSubThoroughfare.ItemData.Detail := Address.SubThoroughfare;  
  22.   
  23.   ListBoxItemThoroughfare.ItemData.Detail    := Address.Thoroughfare;  
  24.   
  25. end;  
原创粉丝点击